Bridge(桥模式)
把抽象层次结构从其实现中分离出来,使其能够独立改变。抽象层定义了提供客户端使用的上层抽象接口。实现层次结构定义了供抽象层次使用的底层接口。实现类的引用被封装与抽象层的实例中。
比如项目中Web与iOS/android的交互。Web只负责一套抽象的接口(Abstraction),在iOS环境和安卓环境有不同的实现(Implementor)来实现对应的功能,具体如何实现Web压根不用管。
VC.m
HCDPhone *phone = [[HCDNokiaPhone alloc]init];
[phone play];
[phone work];
[phone game];
// 抽象层提供抽象接口
HCDNokiaPhone.m
@implementation HCDNokiaPhone
@synthesize gameSoft = _gameSoft;
@synthesize workSoft = _workSoft;
@synthesize playSoft =_playSoft;
-(instancetype)init{
self = [super init];
if (self) {
_gameSoft = [[HCDGameSoftware alloc]init];
_workSoft = [[HCDWorkSoftWare alloc]init];
_playSoft = [[HCDPlaySoftWare alloc]init];
}
return self;
}
-(void)work{
[self.workSoft runXcode];
[self.workSoft runWord];
}
-(void)play{
[self.playSoft runWeixin];
[self.playSoft runQQ];
}
-(void)game{
[self.gameSoft runQQDizhu];
[self.gameSoft runQQMajiang];
}
@end
// 实现层定义了抽象层使用的底层接口
HCDSoftware.h
@protocol HCDSoftware <NSObject>
-(void)runQQ;
-(void)runWeixin;
-(void)runWord;
-(void)runXcode;
-(void)runQQDizhu;
-(void)runQQMajiang;
@end
typedef id<HCDSoftware> HCDSoftware;