标签:ffffff ram eem image div tor 要求 int nonatomic
定义
抽象出层次结构。
上层抽象接口的职能,实现上层抽象接口的职能,层级间的通信协议(可以抽象为接口)。
桥接模式的目的,就是把抽象层次结构从具体的实现中分离出来,使其能够独立变更。抽象层次定义了供客户端使用的上层抽象接口。实现结构定义了供抽象层使用的底层接口。实现类的引用被封装到控制类抽象层的实例中,桥接就形成了。
使用场景
游戏机模拟器、H5混编解决方案
实现步骤:
结构图:
#import <Foundation/Foundation.h> #import "ConsoleEmulator.h" @interface ConsoleController : NSObject /** * 抽象模拟器 */ @property (nonatomic, strong) ConsoleEmulator *emulator; /** * 执行指令 * * @param command 指令 */ - (void)excuteCommand:(ConsoleCommand)command; @end
#import "ConsoleController.h" @implementation ConsoleController - (void)excuteCommand:(ConsoleCommand)command { [_emulator loadInstructionsForCommand:command]; [_emulator excuteInstructions]; } @end
#import <Foundation/Foundation.h> typedef enum : NSUInteger { kConsoleCommandUp, kConsoleCommandDown, kConsoleCommandLeft, kConsoleCommandRight, kConsoleCommandSelect, kConsoleCommandStart, kConsoleCommandAction1, kConsoleCommandAction2, } ConsoleCommand; @interface ConsoleEmulator : NSObject /** * 加载指令 * * @param command 指令 */ - (void)loadInstructionsForCommand:(ConsoleCommand)command; /** * 执行指令 */ - (void)excuteInstructions; @end
#import "ConsoleEmulator.h" @implementation ConsoleEmulator - (void)loadInstructionsForCommand:(ConsoleCommand)command { // 由子类重载实现 } - (void)excuteInstructions { // 由子类重载实现 } @end
#import "ConsoleController.h" @interface GameBoyConsoleController : ConsoleController - (void)up; - (void)down; - (void)left; - (void)right; - (void)select; - (void)start; - (void)action1; - (void)action2; @end
#import "GameBoyConsoleController.h" @implementation GameBoyConsoleController - (void)up { [super excuteCommand:kConsoleCommandUp]; } - (void)down { [super excuteCommand:kConsoleCommandDown]; } - (void)left { [super excuteCommand:kConsoleCommandLeft]; } - (void)right { [super excuteCommand:kConsoleCommandRight]; } - (void)select { [super excuteCommand:kConsoleCommandSelect]; } - (void)start { [super excuteCommand:kConsoleCommandStart]; } - (void)action1 { [super excuteCommand:kConsoleCommandAction1]; } - (void)action2 { [super excuteCommand:kConsoleCommandAction2]; } @end
#import "ConsoleEmulator.h" @interface GameBoyEmulator : ConsoleEmulator - (void)loadInstructionsForCommand:(ConsoleCommand)command; - (void)excuteInstructions; @end
#import "GameBoyEmulator.h" @implementation GameBoyEmulator - (void)loadInstructionsForCommand:(ConsoleCommand)command { NSLog(@"GameBoyEmulator loadInstructionsForCommand"); } - (void)excuteInstructions { NSLog(@"GameBoyEmulator excute"); } @end
2019-09-08 00:27:13.226801+0800 BridgePattern[26512:6943326] GameBoyEmulator loadInstructionsForCommand 2019-09-08 00:27:13.226970+0800 BridgePattern[26512:6943326] GameBoyEmulator excute 2019-09-08 00:27:13.227075+0800 BridgePattern[26512:6943326] GameGearEmulator loadInstructionsForCommand 2019-09-08 00:27:13.227162+0800 BridgePattern[26512:6943326] GameGearEmulator excute
桥接模式
标签:ffffff ram eem image div tor 要求 int nonatomic
原文地址:https://www.cnblogs.com/lxlx1798/p/11484069.html