桥接模式---把两个相关联的类抽象出来, 以达到解耦的目的

比如XBox遥控器跟XBox主机, 我们抽象出主机和遥控器两个抽象类, 让这两个抽象类耦合

然后生成这两个抽象类的实例XBox & XBox主机 以达到解耦

同时还能再继承为其他的游戏机

因为是控制器在控制主机, 所以控制器抽象类会持有主机抽象类

BaseControl.h

 #import <Foundation/Foundation.h>
#import "BaseGameSystem.h" @interface BaseControl : NSObject @property (nonatomic, strong) BaseGameSystem *gameSystem; /**
上下左右AB-方法
*/
- (void)up;
- (void)down;
- (void)left;
- (void)right;
- (void)commandA;
- (void)commandB; @end

BaseControl.m

 #import "BaseControl.h"

 @implementation BaseControl

 - (void)up {

     [self.gameSystem loadComand:kUp];
} - (void)down { [self.gameSystem loadComand:kDown];
} - (void)left { [self.gameSystem loadComand:kLeft];
} - (void)right { [self.gameSystem loadComand:kRight];
} - (void)commandA { [self.gameSystem loadComand:kA];
} - (void)commandB { [self.gameSystem loadComand:kB];
} @end

BaseGameSystem.h

 #import <Foundation/Foundation.h>

 typedef enum : NSUInteger {

     kUp = 0x1,
kDown,
kLeft,
kRight,
kA,
kB, kO,
kX } EcomandType; @interface BaseGameSystem : NSObject /**
加载指令 @param type 指令代码
*/
- (void)loadComand:(EcomandType) type; @end

BaseGameSystem.m

 #import "BaseGameSystem.h"

 @implementation BaseGameSystem

 - (void)loadComand:(EcomandType)type {

 }

 @end

这样两个抽象类就耦合了, 现在我们创建实例

XBoxGameSystem.h

 #import "BaseGameSystem.h"

 @interface XBoxGameSystem : BaseGameSystem

 @end

XboxGameSystem.m

 #import "XBoxGameSystem.h"

 @implementation XBoxGameSystem

 - (void)loadComand:(EcomandType)type {

     switch (type) {
case kUp:
NSLog(@"UP");
break; case kDown:
NSLog(@"Down");
break; case kRight:
NSLog(@"Right");
break; case kLeft:
NSLog(@"Left");
break; case kA:
NSLog(@"kA");
break; case kB:
NSLog(@"kB");
break; default:
NSLog(@"None");
break;
}
}
@end

XBoxController.h

 #import "BaseControl.h"

 @interface XBoxController : BaseControl

 @end

XBoxController.m

 #import "XBoxController.h"

 @implementation XBoxController

 @end

下面是Controller中使用

 #import "ViewController.h"
#import "BaseControl.h"
#import "XBoxGameSystem.h"
#import "XBoxController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; BaseControl *controller = [[XBoxController alloc] init];
controller.gameSystem = [[XBoxGameSystem alloc] init]; [controller up]; } @end
05-06 16:55