基本概念
適配器模式是將一個類的接口轉(zhuǎn)換成客戶希望的另外一個接口径簿。Adapter模式使得原本由于接口不能兼容而不能一起工作的那些類可以一起工作。
系統(tǒng)的數(shù)據(jù)和行為都正確枪汪,但是接口不符(對應(yīng)的方法)被去,我們應(yīng)該考慮適配器,目的是使控制范圍之外的一個原有對象與某個接口匹配暑诸。適配器模式主要是應(yīng)用于希望復用一些現(xiàn)存的類,但是接口又與復用的環(huán)境要求不一致辟灰。
代碼的實現(xiàn)
籃球中有前鋒中鋒和后衛(wèi)个榕,都是球員,抽象出一個球員類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
前鋒芥喇,中鋒西采,后衛(wèi)類
前鋒
#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);
}
后衛(wèi)
#import "Guards.h"
@implementation Guards
- (void)attack
{
NSLog(@"后衛(wèi)%@進攻",self.name);
}
- (void)defense
{
NSLog(@"后衛(wèi)%@防守",self.name);
}
客戶端實現(xiàn),教練下達命令
Player *f = [[Forwards alloc] initWithName:@"巴蒂爾"];
[f attack];
Player *c = [[Center alloc] initWithName:@"麥克格雷迪"];
[c attack];
Player *g = [[Guards alloc] initWithName:@"姚明"];
[g defense];
[g attack];
輸出結(jié)果:
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] 后衛(wèi)姚明防守
2017-04-14 10:29:12.455 原型模式[43252:571756] 后衛(wèi)姚明進攻
但是姚明是個外籍球員如下继控,創(chuàng)建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(@"外籍后衛(wèi)%@進攻",self.foreginName);
}
- (void)foreignDefense
{
NSLog(@"外籍后衛(wèi)%@防守",self.foreginName);
}
@end
他需要個翻譯將教練的話轉(zhuǎn)為他可以聽得懂的械馆,創(chuàng)建Translator類,將教練的話轉(zhuǎn)為他可以聽得懂的話
@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
通過翻譯的解釋武通,他就可以聽得懂教練的指揮霹崎,客戶端實現(xiàn)
Player *f = [[Forwards alloc] initWithName:@"巴蒂爾"];
[f attack];
Player *c = [[Center alloc] initWithName:@"麥克格雷迪"];
[c attack];
Player *g = [[Translator alloc] initWithName:@"姚明"];
[g defense];
[g attack];
輸出結(jié)果
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] 外籍后衛(wèi)姚明防守
2017-04-14 10:35:00.000 原型模式[43338:576501] 外籍后衛(wèi)姚明進攻