適配器模式學(xué)習(xí)
理解
看了YouXianMing的github感覺需要加深一下印象。
使用適配器的前提是之前有一套完整的邏輯其垄,然后新添加了一下之前無法滿足的邏輯,這個時候可以使用適配器卤橄。如果之前沒有一套完整的已經(jīng)存在的邏輯绿满,不建議直接使用。因?yàn)椴恢篮罄m(xù)會不會有改動窟扑。萬一之后都沒改動呢.....
目前只能想到
當(dāng)view對應(yīng)了好多個model類的時候用來隔離view和model的一種方法喇颁。
首先定義一個protocol協(xié)議漏健,
其中聲明view獲取具體數(shù)據(jù)的方法
@protocol AdapterProtocol <NSObject>
- (NSString *)name;
- (NSString *)phoneNumber;
@end
適配器遵守該協(xié)議,并實(shí)現(xiàn)其中的方法橘霎。
適配器基類
@interface BusinessCardAdapter : NSObject <AdapterProtocol>
/**
* 輸入對象
*/
@property (nonatomic, weak) id data;
/**
* 與輸入對象建立聯(lián)系
*
* @param data 輸入的對象
*
* @return 實(shí)例對象
*/
- (instancetype)initWithData:(id)data;
@end
@implementation BusinessCardAdapter
- (instancetype)initWithData:(id)data {
if (self = [super init]) {
self.data = data;
}
return self;
}
- (NSString *)name {
return nil;
}
- (NSString *)phoneNumber {
return nil;
}
@end
不同的model類型
- NormalModel
@interface NormalModel : NSObject
/**
* 名字
*/
@property (nonatomic, strong) NSString *name;
/**
* 線條顏色
*/
@property (nonatomic, strong) UIColor *lineColor;
/**
* 電話號碼
*/
@property (nonatomic, strong) NSString *phoneNumber;
@end
- SpecialModel
@interface SpecialModel : NSObject
/**
* 名字
*/
@property (nonatomic, strong) NSString *name;
/**
* 線條顏色
*/
@property (nonatomic, strong) NSString *colorString;
/**
* 電話號碼
*/
@property (nonatomic, strong) NSString *phoneNumber;
@end
可以看到兩個數(shù)據(jù)模型中表示顏色的部分是不同的蔫浆。NormalModel中是標(biāo)準(zhǔn)的顏色對象表示顏色,SpecialModel中是顏色字符串表示顏色姐叁。
對象適配器
對象適配器就是在同一個適配器類中對傳入的數(shù)據(jù)進(jìn)行數(shù)據(jù)適配瓦盛。
@interface CommonUsedAdapter : BusinessCardAdapter
@end
@implementation CommonUsedAdapter
- (NSString *)name {
NSString *name = nil;
if ([self.data isMemberOfClass:[SpecialModel class]]) {
SpecialModel *model = self.data;
name = model.name;
} else if ([self.data isMemberOfClass:[NormalModel class]]) {
NormalModel *model = self.data;
name = model.name;
} else {
name = nil;
}
return name;
}
- (UIColor *)lineColor {
UIColor *lineColor = nil;
if ([self.data isMemberOfClass:[SpecialModel class]]) {
SpecialModel *model = self.data;
if ([model.colorString isEqualToString:@"red"]) {
lineColor = [UIColor redColor];
} else if ([model.colorString isEqualToString:@"green"]) {
lineColor = [UIColor greenColor];
} else {
lineColor = [UIColor blackColor];
}
} else if ([self.data isMemberOfClass:[NormalModel class]]) {
NormalModel *model = self.data;
lineColor = model.lineColor;
} else {
lineColor = nil;
}
return lineColor;
}
- (NSString *)phoneNumber {
NSString *phoneNumber = nil;
if ([self.data isMemberOfClass:[SpecialModel class]]) {
SpecialModel *model = self.data;
phoneNumber = model.phoneNumber;
} else if ([self.data isMemberOfClass:[NormalModel class]]) {
NormalModel *model = self.data;
phoneNumber = model.phoneNumber;
} else {
phoneNumber = nil;
}
return phoneNumber;
}
@end
view對應(yīng)該適配器,通過調(diào)用協(xié)議的方法來獲取數(shù)據(jù)外潜。
.h
/**
* 加載數(shù)據(jù)(實(shí)現(xiàn)了BusinessCardAdapterProtocol協(xié)議的數(shù)據(jù))
*
* @param data 實(shí)現(xiàn)了BusinessCardAdapterProtocol協(xié)議的數(shù)據(jù)
*/
- (void)loadData:(id <AdapterProtocol>)data;
從適配器中獲取數(shù)據(jù)
.m
- (void)loadData:(id <AdapterProtocol>)data {
self.name = [data name];
self.lineColor = [data lineColor];
self.phoneNumber = [data phoneNumber];
}
在外界初始化view之后原环,給view添加數(shù)據(jù)的時候,
首先根據(jù)具體的數(shù)據(jù)模型model來初始化適配器处窥,
然后將適配器傳給view
view不關(guān)心具體數(shù)據(jù)的類型和邏輯嘱吗,只做顯示作用。
// 數(shù)據(jù)源
NormalModel *data1 = [[NormalModel alloc] init];
data1.name = @"YouXianMing";
data1.lineColor = [UIColor redColor];
data1.phoneNumber = @"159 - 1051 - 4636";
// 數(shù)據(jù)源
SpecialModel *data2 = [[SpecialModel alloc] init];
data2.name = @"NoZuoNoDie";
data2.colorString = @"green";
data2.phoneNumber = @"159 - 1051 - 4636";
// 通用型適配器類(可以加載數(shù)據(jù)data1或者data2)
//BusinessCardAdapter *adapter = [[CommonUsedAdapter alloc] initWithData:data1];
BusinessCardAdapter *adapter = [[CommonUsedAdapter alloc] initWithData:data2];
BusinessCardView *cardView = [[BusinessCardView alloc] initWithFrame:BUSINESS_FRAME];
cardView.center = self.view.center;
[cardView loadData:adapter];
邏輯由適配器根據(jù)具體數(shù)據(jù)處理滔驾。
類適配器
類適配器是針對每一種model對應(yīng)一個繼承于適配器基類的子適配器類谒麦。
- SpecialModel對應(yīng)的適配器
@interface SpecialModelAdapter : BusinessCardAdapter
@end
@implementation SpecialModelAdapter
- (NSString *)name {
SpecialModel *model = self.data;
return model.name;
}
- (UIColor *)lineColor {
SpecialModel *model = self.data;
UIColor *color = nil;
if ([model.colorString isEqualToString:@"red"]) {
color = [UIColor redColor];
} else if ([model.colorString isEqualToString:@"green"]) {
color = [UIColor greenColor];
} else {
color = [UIColor blackColor];
}
return color;
}
- (NSString *)phoneNumber {
SpecialModel *model = self.data;
return model.phoneNumber;
}
@end
- NormalModel對應(yīng)的適配器
@interface NormalModelAdapter : BusinessCardAdapter
@end
@implementation NormalModelAdapter
- (NSString *)name {
NormalModel *model = self.data;
return model.name;
}
- (UIColor *)lineColor {
NormalModel *model = self.data;
return model.lineColor;
}
- (NSString *)phoneNumber {
NormalModel *model = self.data;
return model.phoneNumber;
}
@end
在外界給view填充數(shù)據(jù)的時候:
- 一種
// 數(shù)據(jù)源
NormalModel *data = [[NormalModel alloc] init];
data.name = @"YouXianMing";
data.lineColor = [UIColor redColor];
data.phoneNumber = @"159 - 1051 - 4636";
// 適配器類
BusinessCardAdapter *adapter = [[NormalModelAdapter alloc] initWithData:data];
BusinessCardView *cardView = [[BusinessCardView alloc] initWithFrame:BUSINESS_FRAME];
cardView.center = self.view.center;
[cardView loadData:adapter];
- 另一種
// 數(shù)據(jù)源
SpecialModel *data = [[SpecialModel alloc] init];
data.name = @"NoZuoNoDie";
data.colorString = @"green";
data.phoneNumber = @"159 - 1051 - 4636";
// 適配器類
BusinessCardAdapter *adapter = [[SpecialModelAdapter alloc] initWithData:data];
BusinessCardView *cardView = [[BusinessCardView alloc] initWithFrame:BUSINESS_FRAME];
cardView.center = self.view.center;
[cardView loadData:adapter];