介绍部分引用来自:
http://blog.csdn.net/casablaneca/article/details/39860811
1.什么是适配器设计模式(Adapter)
适配器设计模式是一种结构型设计模式, 它的作用是把一个类的接口转换成客户希望的另外一个接口,从而使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
适配器模式有对象适配器和类适配器两种形式的实现结构,但是类适配器采用“多继承”的实现方式,带来了不良的高耦合,所以一般不推荐使用,另外, OC 中也不支持多重继承。对象适配器采用“对象组合”的方式,更符合松耦合规范。
2.如何使用适配器设计模式
1.) 创建一个命令行程序,一个继承自NSObject的Adaptee类,该类中有一个方法-(void)doSomething,我们假定客户端为main函数,main函数不能直接调用该方法,这是我们需要创建一个Adapter类,并定义一个Target协议,协议中定义一个接口-(void)usePublicInterface;
2.) 让Adapter继承Target协议,并与Adaptee关联起来,然后实现协议中定义的接口,在协议中调用Adaptee类中的方法。
3.)在客户端通过Adapter调用usePublicInterface方法就能实现对Adaptee中-(void)doSomething方法的调用。
3.代码实现部分(本人提供参考例子)
第一种形式:
需要创建以下文件:
NormalModel1.h/NormalModel1.m
#import <Foundation/Foundation.h>
@interface NormalModel1 : NSObject
- (void)show;
@end
-------------------------------------------------
#import "NormalModel1.h"
@implementation NormalModel1
- (void)show{
NSLog(@"%s",__func__);
}
@end
NormalModel2.h/NormalModel2.m
#import <Foundation/Foundation.h>
@interface NormalModel2 : NSObject
- (void)show;
@end
-------------------------------------------------
#import "NormalModel2.h"
@implementation NormalModel2
- (void)show{
NSLog(@"%s",__func__);
}
@end
NomalModelProtocol.h
@protocol NomalModelProtocol <NSObject>
- (void)showLog;
@end
NormalContentAdapter.h/NormalContentAdapter.m
#import <Foundation/Foundation.h>
#import "NomalModelProtocol.h"
@interface NormalContentAdapter : NSObject<NomalModelProtocol>
/**
* 输入对象
*/
@property (nonatomic, weak) id data;
/**
* 与输入对象建立联系
*
* @param data 输入的对象
*
* @return 实例对象
*/
- (instancetype)initWithData:(id)data;
@end
-------------------------------------------------
#import "NormalContentAdapter.h"
#import "NormalModel1.h"
#import "NormalModel2.h"
@implementation NormalContentAdapter
- (instancetype)initWithData:(id)data{
self = [super init];
if (self) {
self.data = data;
}
return self;
}
- (void)showLog{
if ([self.data isMemberOfClass:[NormalModel1 class]]) {
NormalModel1 *model = self.data;
[model show];
}else{
NormalModel2 *model = self.data;
[model show];
}
}
@end
调用:
NormalModel1 *model1 = [NormalModel1 new];
NormalContentAdapter *adapter = [[NormalContentAdapter alloc]initWithData:model1];
[adapter showLog];
NormalModel2 *model2 = [NormalModel2 new];
NormalContentAdapter *adapter2 = [[NormalContentAdapter alloc]initWithData:model2];
[adapter2 showLog];
Result:
2017-03-27 21:19:40.981 MVPDemo[664:20823] -[NormalModel1 show]
2017-03-27 21:19:40.982 MVPDemo[664:20823] -[NormalModel2 show]
第二种形式:
需要创建以下文件:
ZWProtocol.h
@protocol ZWProtocol <NSObject>
- (void)show;
@end
ZWModel1.h/ZWModel1.m
#import <Foundation/Foundation.h>
#import "ZWProtocol.h"
@interface ZWModel1 : NSObject<ZWProtocol>
@end
-------------------------------------------------
#import "ZWModel1.h"
@implementation ZWModel1
- (void)show{
NSLog(@"%s",__func__);
}
@end
ZWModel2.h/ZWModel2.m
#import <Foundation/Foundation.h>
#import "ZWProtocol.h"
@interface ZWModel2 : NSObject<ZWProtocol>
@end
-------------------------------------------------
#import "ZWModel2.h"
@implementation ZWModel2
- (void)show{
NSLog(@"%s",__func__);
}
@end
ZWAdaper.h/ZWAdaper.m
#import <Foundation/Foundation.h>
#import "ZWProtocol.h"
@interface ZWAdaper : NSObject
- (instancetype)initWith:(id<ZWProtocol>)adaper;
- (void)showNow;
@end
-------------------------------------------------
#import "ZWAdaper.h"
@interface ZWAdaper ()
@property(nonatomic) id<ZWProtocol> adaper;
@end
@implementation ZWAdaper
- (instancetype)initWith:(id<ZWProtocol>)adaper{
if (self = [super init]) {
self.adaper = adaper;
}
return self;
}
- (void)showNow{
[self.adaper show];
}
@end
调用:
ZWModel1 *model1 = [[ZWModel1 alloc]init];
ZWModel2 *model2 = [[ZWModel2 alloc]init];
ZWAdaper *adaper = [[ZWAdaper alloc]initWith:model1];
[adaper showNow];
ZWAdaper *adaper2 = [[ZWAdaper alloc]initWith:model2];
[adaper2 showNow];
Result:
2017-03-27 21:49:04.678 MVPDemo[712:32009] -[ZWModel1 show]
2017-03-27 21:49:04.678 MVPDemo[712:32009] -[ZWModel2 show]
方式的差别:
- 1 第一种需要做类型判断,第二种不需要
- 2 第一种可能需要使用过多的文件和继承,第二种实现协议就可以
实现的写法虽有不同,思想其实是一致的,不过本人还是比较喜欢第二种方式,第二种不需要使用额外的类型判断,用法更轻巧。