IOS design mode-Bridging
We recommend you refer to the OBJECTIVE-C design mode for learning design patterns.
When to use the Bridge Mode
1. Do not want to form a fixed binding relationship between the abstraction and its implementation (so that the implementation can be switched during runtime)
2. abstraction and its implementation should be able to be independently extended through subclass;
3. Modifying abstract implementations should not affect client code
4. if each implementation requires additional subclass to refine the abstraction, it is necessary to divide them into two parts.
5. share an implementation between multiple objects with different abstract Interfaces
The following code describes the design mode.
First, display the called code
//// ViewController. m // BridgeTest /// Created by qitmac000246 on 15/7/13. // Copyright (c) June 2015 dujia. all rights reserved. // # import ViewController. h # import TouchConsoleController. h # import GameBoyEmulator. h @ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // sub-function implementation GameBoyEmulator * gbe = [[GameBoyEmulator alloc] init]; // the function module calls TouchConsoleController * tcc = [[TouchConsoleController alloc] init]; [tcc setEmulator: gbe]; // specify the function object [tcc setCommand: kConsoleUp]; // Implementation}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end
ConsoleEmulator. h
/// ConsoleEmulator. h // BridgeTest /// Created by qitmac000246 on 15/7/14. // Copyright (c) February 22, 2015 dujia. All rights reserved. // # import
Typedef enum {kConsoleUp,} ConsoleType; // abstract parent class of the execution class @ interface ConsoleEmulator: NSObject // Abstract Public method-(void) loadInstructionsForCommand; @ end
ConsoleEmulator. m
#import ConsoleEmulator.h@implementation ConsoleEmulator- (void) loadInstructionsForCommand{ }@end
GameBoyEmulator. h
#import ConsoleEmulator.h@interface GameBoyEmulator : ConsoleEmulator@end
GameBoyEmulator. m
# Import GameBoyEmulator. h @ implementation GameBoyEmulator // sub-execution class-(void) loadInstructionsForCommand {NSLog (@ % s ,__ func _);} @ end
ConsoleController. h
# Import
# Import ConsoleEmulator. h @ interface ConsoleController: NSObject // define the abstract parent class of the execution class in the parent class of the controller @ property (nonatomic, strong) ConsoleEmulator * emulator; // Abstract Public call method-(void) setCommand :( ConsoleType) command; @ end
ConsoleController. m
#import ConsoleController.h@implementation ConsoleController- (void) setCommand:(ConsoleType)command{ [_emulator loadInstructionsForCommand];}@end
TouchConsoleController. h
# Import ConsoleController. h // controller subclass @ interface TouchConsoleController: ConsoleController-(void) up; @ end
TouchConsoleController. m
#import TouchConsoleController.h@implementation TouchConsoleController- (void) up{ [super setCommand:kConsoleUp];}@end