美文网首页
适配器模式(Adapter)

适配器模式(Adapter)

作者: 傻傻小萝卜 | 来源:发表于2017-04-14 10:36 被阅读17次

基本概念

适配器模式是将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不能兼容而不能一起工作的那些类可以一起工作。

系统的数据和行为都正确,但是接口不符(对应的方法),我们应该考虑适配器,目的是使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要是应用于希望复用一些现存的类,但是接口又与复用的环境要求不一致。

代码的实现

篮球中有前锋中锋和后卫,都是球员,抽象出一个球员类Player

@interface Player : NSObject

@property (nonatomic, strong)NSString *name;

- (instancetype)initWithName:(NSString *)name;

- (void)attack;// 进攻

- (void)defense;// 防守

@end

#import "Player.h"

@implementation Player

- (instancetype)initWithName:(NSString *)name

{

self = [super init];

if (self) {

self.name = name;

}

return self;

}

@end

前锋,中锋,后卫类

前锋

#import "Forwards.h"

@implementation Forwards

- (void)attack

{

NSLog(@"前锋%@进攻",self.name);

}

- (void)defense

{

NSLog(@"前锋%@防守",self.name);

}

@end

中锋

#import "Center.h"

@implementation Center

- (void)attack

{

NSLog(@"中锋%@进攻",self.name);

}

- (void)defense

{

NSLog(@"中锋%@防守",self.name);

}

后卫

#import "Guards.h"

@implementation Guards

- (void)attack

{

NSLog(@"后卫%@进攻",self.name);

}

- (void)defense

{

NSLog(@"后卫%@防守",self.name);

}

客户端实现,教练下达命令

Player *f = [[Forwards alloc] initWithName:@"巴蒂尔"];

[f attack];

Player *c = [[Center alloc] initWithName:@"麦克格雷迪"];

[c attack];

Player *g = [[Guards alloc] initWithName:@"姚明"];

[g defense];

[g attack];

输出结果:

2017-04-14 10:29:12.454 原型模式[43252:571756] 前锋巴蒂尔进攻

2017-04-14 10:29:12.455 原型模式[43252:571756] 中锋麦克格雷迪进攻

2017-04-14 10:29:12.455 原型模式[43252:571756] 后卫姚明防守

2017-04-14 10:29:12.455 原型模式[43252:571756] 后卫姚明进攻

但是姚明是个外籍球员如下,创建ForeignPlayer

@interface ForeignPlayer : NSObject

@property (nonatomic, strong)NSString *foreginName;

- (instancetype)initWithName:(NSString *)name;

- (void)foreignAttack;

- (void)foreignDefense;

@end

#import "ForeignPlayer.h"

@implementation ForeignPlayer

- (instancetype)initWithName:(NSString *)name

{

self = [super init];

if (self) {

self.foreginName = name;

}

return self;

}

- (void)foreignAttack

{

NSLog(@"外籍后卫%@进攻",self.foreginName);

}

- (void)foreignDefense

{

NSLog(@"外籍后卫%@防守",self.foreginName);

}

@end

他需要个翻译将教练的话转为他可以听得懂的,创建Translator类,将教练的话转为他可以听得懂的话

@interface Translator : Player

@end

#import "Translator.h"

#import "ForeignPlayer.h"

@interface Translator()

@property (nonatomic, strong)ForeignPlayer *foreginPlayer;

@end

@implementation Translator

- (instancetype)initWithName:(NSString *)name

{

self = [super init];

if (self) {

self.foreginPlayer = [[ForeignPlayer alloc]initWithName:name];

}

return self;

}

- (void)attack

{

[self.foreginPlayer foreignAttack];

}

- (void)defense

{

[self.foreginPlayer foreignDefense];

}

@end

通过翻译的解释,他就可以听得懂教练的指挥,客户端实现

Player *f = [[Forwards alloc] initWithName:@"巴蒂尔"];

[f attack];

Player *c = [[Center alloc] initWithName:@"麦克格雷迪"];

[c attack];

Player *g = [[Translator alloc] initWithName:@"姚明"];

[g defense];

[g attack];

输出结果

2017-04-14 10:34:59.996 原型模式[43338:576501] 前锋巴蒂尔进攻

2017-04-14 10:34:59.999 原型模式[43338:576501] 中锋麦克格雷迪进攻

2017-04-14 10:34:59.999 原型模式[43338:576501] 外籍后卫姚明防守

2017-04-14 10:35:00.000 原型模式[43338:576501] 外籍后卫姚明进攻

相关文章

网友评论

      本文标题:适配器模式(Adapter)

      本文链接:https://www.haomeiwen.com/subject/vbzbattx.html