桥接模式
抽象出层次结构。
上层抽象接口的职能,实现上层抽象接口的职能,层级间的通信协议(可以抽象为接口)。
桥接模式的目的,就是把抽象层次结构从具体的实现中分离出来,使其能够独立变更。抽象层次定义了供客户端使用的上层抽象接口。实现结构定义了供抽象层使用的底层接口。实现类的引用被封装到控制类抽象层的实例中,桥接就形成了。应用,使用场景
游戏机模拟器、H5混编解决方案
游戏机系统抽象类(控制类/协议)
//
// AbstractSystem.h
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "AbstractImplementor.h"
/**
控制类
*/
@interface AbstractSystem : NSObject
@property (strong, nonatomic)AbstractImplementor *implementor;
/**
加载系统
*/
- (void)loadSystem;
- (void)commandUp;
- (void)commandDown;
- (void)commandLeft;
- (void)commandRight;
- (void)commandA;
- (void)commandB;
@end
//
// AbstractSystem.m
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import "AbstractSystem.h"
@implementation AbstractSystem
- (void)loadSystem {
}
- (void)commandUp {
[self.implementor loadCommand:kUp];
}
- (void)commandDown {
[self.implementor loadCommand:kDown];
}
- (void)commandLeft {
[self.implementor loadCommand:kLeft];
}
- (void)commandRight {
[self.implementor loadCommand:kRight];
}
- (void)commandA {
[self.implementor loadCommand:kA];
}
- (void)commandB {
[self.implementor loadCommand:kB];
}
@end
游戏机执行抽象类(执行类/协议)
//
// AbstractImplementor.h
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef enum : NSInteger {
kUp,
kDown,
kLeft,
kRight,
kA,
kB,
kO,
kX,
} ECommandType;
/**
执行类
*/
@interface AbstractImplementor : NSObject
- (void)loadCommand:(ECommandType)command;
@end
//
// AbstractImplementor.m
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import "AbstractImplementor.h"
@implementation AbstractImplementor
- (void)loadCommand:(ECommandType)command {
}
@end
GBA游戏机系统类
//
// GBASystem.h
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import "AbstractSystem.h"
@interface GBASystem : AbstractSystem
@end
//
// GBASystem.m
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import "GBASystem.h"
@implementation GBASystem
- (void)loadSystem {
NSLog(@"GBASystem");
}
@end
GBA执行类
//
// GBAImplementor.h
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import "AbstractImplementor.h"
@interface GBAImplementor : AbstractImplementor
@end
//
// GBAImplementor.m
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import "GBAImplementor.h"
@implementation GBAImplementor
- (void)loadCommand:(ECommandType)command {
switch (command) {
case kUp:
NSLog(@"GBA up");
break;
case kDown:
NSLog(@"GBA down");
break;
case kLeft:
NSLog(@"GBA left");
break;
case kRight:
NSLog(@"GBA right");
break;
case kA:
NSLog(@"GBA A");
break;
case kB:
NSLog(@"GBA B");
break;
default:
NSLog(@"GBA none");
break;
}
}
@end
PSP游戏机系统类(扩展X、O)
//
// PSPSystem.h
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import "AbstractSystem.h"
@interface PSPSystem : AbstractSystem
- (void)commandX;
- (void)commandO;
@end
//
// PSPSystem.m
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import "PSPSystem.h"
@implementation PSPSystem
- (void)loadSystem {
NSLog(@"PSPSystem");
}
- (void)commandX {
[self.implementor loadCommand:kX];
}
- (void)commandO {
[self.implementor loadCommand:kO];
}
@end
PSP执行类
//
// PSPImplementor.h
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import "AbstractImplementor.h"
@interface PSPImplementor : AbstractImplementor
@end
//
// PSPImplementor.m
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import "PSPImplementor.h"
@implementation PSPImplementor
- (void)loadCommand:(ECommandType)command {
switch (command) {
case kUp:
NSLog(@"PSP up");
break;
case kDown:
NSLog(@"PSP down");
break;
case kLeft:
NSLog(@"PSP left");
break;
case kRight:
NSLog(@"PSP right");
break;
case kA:
NSLog(@"PSP A");
break;
case kB:
NSLog(@"PSP B");
break;
case kX:
NSLog(@"PSP X");
break;
case kO:
NSLog(@"PSP O");
break;
default:
NSLog(@"PSP none");
break;
}
}
@end
应用
//
// ViewController.m
// LearnBridge
//
// Created by 印林泉 on 2017/3/7.
// Copyright © 2017年 ylq. All rights reserved.
//
#import "ViewController.h"
#import "GBASystem.h"
#import "GBAImplementor.h"
#import "PSPSystem.h"
#import "PSPImplementor.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
AbstractSystem *gbaSystem = [[GBASystem alloc] init];
gbaSystem.implementor = [[GBAImplementor alloc] init];
[gbaSystem loadSystem];
[gbaSystem commandUp];
PSPSystem *pspSystem = [[PSPSystem alloc] init];
pspSystem.implementor = [[PSPImplementor alloc] init];
[pspSystem loadSystem];
[pspSystem commandX];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end