開發時常遇到這種情況:你寫了一個純展示用的視圖控件,沒有什麼交互,但專案裡好幾個地方都在用它。這個時候,你是怎麼把資料餵給它的?
很直覺的寫法大概像這樣:
-(void)laodData:(ItemModel *)model;
這樣寫沒錯,也很簡單,但有兩個問題:
視圖與資料模型耦合了——視圖類別直接引入了模型。
2. 當專案其他地方要複用這個視圖,但對應的模型換成了 ContentModel,你是不是又得補一個新的初始化方法?
-(void)laodData:(ContentModel *)model;
暫且也沒錯,但如果有更多地方、更多模型要接入呢?換個角度想:我已經寫好的視圖控件,為什麼要因為資料來源不同而不斷改寫?有沒有辦法讓視圖獨立出來,完全不依賴具體模型?這正是適配器模式要處理的問題。
適配器模式的核心是把一個類別的介面轉換成客戶期待的另一個介面,讓原本因介面不相容而無法協同工作的類別可以一起運作。
適用場景:
1、既有的類別介面不符合需求;
2、希望建立一個可複用的類別,能與其他不相關或介面未必相容的類別協同工作;
3、需要在不為每個既有子類別重新封裝的前提下複用它們。
看個具體例子。專案裡有一個視圖類別,左邊是圖片、右邊是一段文字,多處複用且對應不同的資料模型:

把初始化方法改成接收一個遵循特定協議的物件:
-(void)loadData:(id)data{ self.image = [data image]; self.contentStr = [data contentStr]; }
這樣一來,傳進來的物件是什麼型別並不重要,只要遵循協議、實作協議中的方法,視圖就能正常渲染。
@protocol ContentViewAdapterProtocol - (UIImage *)image; - (NSString *)contentStr; @end
這個類別遵循上面的協議並實作協議方法,但只做空實作:
- (UIImage *)image{
return nil;
}
- (NSString *)contentStr{
return nil;
}再加上一個初始化資料的方法:
- (instancetype)initWithData:(id)data;
這樣根適配器就完成了。
適配器分為類適配器與物件適配器兩種做法。類適配器是為每一個資料類別各建一個適配器,都繼承自上面的根適配器;要接入新模型就再新增一個適配器類別。
針對 ItemModel 的適配器:
#import "ItemModelAdeapter.h"
#import "ItemModel.h"
@implementation ItemModelAdeapter
- (instancetype)initWithData:(id)data{
self = [super init];
if (self) {
self.data = data;
}
return self;
}
- (UIImage *)image{
ItemModel *model = self.data;
return model.image;
}
- (NSString *)contentStr{
ItemModel *model = self.data;
return model.conntentStr;
}
@end物件適配器則只建立一個適配器類別,在內部依資料型別分派:
#import "ModelAdapter.h"
#import "ItemModel.h"
#import "ContenModel.h"
// 物件適配器
@implementation ModelAdapter
- (instancetype)initWithData:(id)data{
self = [super init];
if (self) {
self.data = data;
}
return self;
}
- (UIImage *)image{
if ([self.data isMemberOfClass:[ContenModel class]]) {
ContenModel *model = self.data;
return [UIImage imageNamed:model.imageName];
} else {
ItemModel *model = self.data;
return model.image;
}
}
- (NSString *)contentStr{
if ([self.data isMemberOfClass:[ContenModel class]]) {
ContenModel *model = self.data;
return model.conntentStr;
} else {
ItemModel *model = self.data;
return model.conntentStr;
}
}
@end物件適配器的優點是所有分派邏輯集中在單一類別中,類別數量較少;缺點是模型一多,方法內部會變得複雜。類適配器則相反:每個模型的適配邏輯各自獨立,但類別數量會隨模型數量線性增加。
類適配器,一次適配兩個類別:
// 第一個 ContenModel *contenModel = [[ContenModel alloc] init]; contenModel.conntentStr = @"時間:10:32:12"; contenModel.imageName = @"shijian"; ContentViewAdapter *modelAdapter = [[ContentModelAdeapter alloc] initWithData:contenModel]; ContentView *contentView = [[ContentView alloc] initWithFrame:CGRectMake(100, 100, 200, 20)]; [contentView loadData:modelAdapter]; [self.view addSubview:contentView]; // 第二個 ItemModel *itemModel = [[ItemModel alloc] init]; itemModel.conntentStr = @"心率:100次"; itemModel.image = [UIImage imageNamed:@"mapHeaderIcon"]; ContentViewAdapter *modelAdapter1 = [[ItemModelAdeapter alloc] initWithData:itemModel]; ContentView *contentView1 = [[ContentView alloc] initWithFrame:CGRectMake(100, 200, 200, 20)]; [contentView1 loadData:modelAdapter1]; [self.view addSubview:contentView1];
物件適配器:
// 第一個 ItemModel *itemModel1 = [[ItemModel alloc] init]; itemModel1.conntentStr = @"心率:100次"; itemModel1.image = [UIImage imageNamed:@"mapHeaderIcon"]; ContentViewAdapter *modelAdapter2 = [[ModelAdapter alloc] initWithData:itemModel]; ContentView *contentView2 = [[ContentView alloc] initWithFrame:CGRectMake(100, 300, 200, 20)]; [contentView2 loadData:modelAdapter2]; [self.view addSubview:contentView2]; // 第二個 ContenModel *contenModel2 = [[ContenModel alloc] init]; contenModel2.conntentStr = @"時間:10:32:12"; contenModel2.imageName = @"shijian"; ContentViewAdapter *modelAdapter3 = [[ModelAdapter alloc] initWithData:contenModel]; ContentView *contentView3 = [[ContentView alloc] initWithFrame:CGRectMake(100, 400, 200, 20)]; [contentView3 loadData:modelAdapter3]; [self.view addSubview:contentView3];
解除了視圖與資料模型之間的耦合,視圖類別變得更獨立;新增資料模型時不需要改動視圖。
會多出不少類別,專案結構變得零碎,程式碼可讀性反而下降。因此仍需視情況斟酌使用。
本文的靈感來自 YouXianMing 的教學,我在自己的專案中實際重構之後整理成這篇筆記,希望對大家有幫助,也在此感謝原作者。
文章轉自 idage 的簡書