标签:抽象类 efault box 目的 object integer end -- div
桥接模式---把两个相关联的类抽象出来, 以达到解耦的目的
比如XBox遥控器跟XBox主机, 我们抽象出主机和遥控器两个抽象类, 让这两个抽象类耦合
然后生成这两个抽象类的实例XBox & XBox主机 以达到解耦
同时还能再继承为其他的游戏机
因为是控制器在控制主机, 所以控制器抽象类会持有主机抽象类
BaseControl.h
1 #import <Foundation/Foundation.h> 2 #import "BaseGameSystem.h" 3 4 @interface BaseControl : NSObject 5 6 @property (nonatomic, strong) BaseGameSystem *gameSystem; 7 8 /** 9 上下左右AB-方法 10 */ 11 - (void)up; 12 - (void)down; 13 - (void)left; 14 - (void)right; 15 - (void)commandA; 16 - (void)commandB; 17 18 @end
BaseControl.m
1 #import "BaseControl.h" 2 3 @implementation BaseControl 4 5 - (void)up { 6 7 [self.gameSystem loadComand:kUp]; 8 } 9 10 - (void)down { 11 12 [self.gameSystem loadComand:kDown]; 13 } 14 15 - (void)left { 16 17 [self.gameSystem loadComand:kLeft]; 18 } 19 20 - (void)right { 21 22 [self.gameSystem loadComand:kRight]; 23 } 24 25 - (void)commandA { 26 27 [self.gameSystem loadComand:kA]; 28 } 29 30 - (void)commandB { 31 32 [self.gameSystem loadComand:kB]; 33 } 34 35 @end
BaseGameSystem.h
1 #import <Foundation/Foundation.h> 2 3 typedef enum : NSUInteger { 4 5 kUp = 0x1, 6 kDown, 7 kLeft, 8 kRight, 9 kA, 10 kB, 11 12 kO, 13 kX 14 15 } EcomandType; 16 17 @interface BaseGameSystem : NSObject 18 19 /** 20 加载指令 21 22 @param type 指令代码 23 */ 24 - (void)loadComand:(EcomandType) type; 25 26 @end
BaseGameSystem.m
1 #import "BaseGameSystem.h" 2 3 @implementation BaseGameSystem 4 5 - (void)loadComand:(EcomandType)type { 6 7 } 8 9 @end
这样两个抽象类就耦合了, 现在我们创建实例
XBoxGameSystem.h
1 #import "BaseGameSystem.h" 2 3 @interface XBoxGameSystem : BaseGameSystem 4 5 @end
XboxGameSystem.m
1 #import "XBoxGameSystem.h" 2 3 @implementation XBoxGameSystem 4 5 - (void)loadComand:(EcomandType)type { 6 7 switch (type) { 8 case kUp: 9 NSLog(@"UP"); 10 break; 11 12 case kDown: 13 NSLog(@"Down"); 14 break; 15 16 case kRight: 17 NSLog(@"Right"); 18 break; 19 20 case kLeft: 21 NSLog(@"Left"); 22 break; 23 24 case kA: 25 NSLog(@"kA"); 26 break; 27 28 case kB: 29 NSLog(@"kB"); 30 break; 31 32 default: 33 NSLog(@"None"); 34 break; 35 } 36 } 37 @end
XBoxController.h
1 #import "BaseControl.h" 2 3 @interface XBoxController : BaseControl 4 5 @end
XBoxController.m
1 #import "XBoxController.h" 2 3 @implementation XBoxController 4 5 @end
下面是Controller中使用
1 #import "ViewController.h" 2 #import "BaseControl.h" 3 #import "XBoxGameSystem.h" 4 #import "XBoxController.h" 5 6 @interface ViewController () 7 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 14 [super viewDidLoad]; 15 16 BaseControl *controller = [[XBoxController alloc] init]; 17 controller.gameSystem = [[XBoxGameSystem alloc] init]; 18 19 [controller up]; 20 21 } 22 23 24 25 @end
标签:抽象类 efault box 目的 object integer end -- div
原文地址:http://www.cnblogs.com/zhouxihi/p/6067898.html