適配器模式

適配器模式(Adapter):將一個類的接口轉換成客戶端但願的另一個接口,適配器模式使得本來因爲接口不兼容而不能一塊兒工做的那些類能夠一塊兒工做。 適配器用於鏈接兩種不一樣種類的對象,使其毫無問題的協同工做。有時也稱做爲包裝器Wapper。思想很簡單,適配器實現客戶端所要的某種接口的行爲。同時,它又鏈接到另外一個具備(徹底)不一樣接口與行爲的對象。一邊是客戶端懂得如何使用的目標接口,另外一邊是客戶端一無所知的被適配者,適配器處於二者之間。適配器的主要做用是把被適配者的行爲傳遞給管道另外一端的客戶端。html

6.1使用場景:

  • 已有類的接口與需求不匹配。
  • 想要一個可複用的類,該類可以同可能帶有不兼容接口的其餘類協做。
  • 須要適配一個類的幾個不一樣子類,但是每個子類去子類化一個類適配器又不現實。那麼可使用對象適配器(也就是代理)來適配其父類的接口。

6.2角色劃分:

三個核心角色: 角色一:適配器(核心) 角色二:目標接口 角色三:被適配者 實際開發中:UITablview 角色一:適配器->ViewController(實現協議->兩個delegate) 角色二:目標接口->UI界面(抽象層面)->UITablview(Cell) 角色三:被適配者->數據(UserModel)app

解決問題:若是把ViewController做爲適配器,更新UI,適配UI,處理邏輯,容易出現代碼臃腫。 解決方案:經過適配器模式優化代碼結構,解決類代碼臃腫。優化

6.3適配器模式-原理案例

適配器模式分爲兩種atom

6.3.1.類適配器

經過案例分析: 金額轉換->1000USA->適配->人民幣6500 適配器:Adapter(適配器) 目標接口:Target 被適配者:Adaptee.net

[@protocol](https://my.oschina.net/u/819710) Target <NSObject>
-(float)getRMB;
[@end](https://my.oschina.net/u/567204)

#import <Foundation/Foundation.h>
#import "Target.h"
#import "Adaptee.h"
//1.適配器實現協議(目標接口)
//2.集成被適配者
[@interface](https://my.oschina.net/u/996807) Adapter : Adaptee<Target>

[@end](https://my.oschina.net/u/567204)

@implementation Adapter
- (float)getRMB{
	return [self getUSA]*6.5;
}
@end

@interface Adaptee : NSObject
-(float)getUSA;
@end

@implementation Adaptee
-(float)getUSA{
	return 1000;
}
@end

6.3.2.對象適配器

#import <Foundation/Foundation.h>
#import "Target.h"
#import "Adaptee.h"
//特色一:實現協議
//特色二:持有被適配者引用
@interface ObjectAdapter : NSObject<Target>
-(instancetype)init:(Adaptee *)adaptee;
@end

#import "ObjectAdapter.h"

@interface ObjectAdapter()
@property(nonatomic , strong) Adaptee* adaptee;
@end
@implementation ObjectAdapter
-(instancetype)init:(Adaptee *)adaptee{
	self = [super init];
	if (self){
		self.adaptee = adaptee;
		
	}
	return self;
}

- (float)getRMB{
	return [self.adaptee getUSA] *6.5;
}
@end

6.3.3 適配器模式-開發案例

角色一:適配器->BaseAdapter->代替ViewController的功能 角色二:目標接口->UI界面(抽象)->UITableView(Cell) 角色三:被適配者->數據(UserModel…)代理

適配器:code

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface BaseAdapter : NSObject<UITableViewDataSource,UITableViewDelegate>
//萬能類型   可存儲各種數據
@property (nonatomic , strong) NSArray *dataArray;
@end

//抽離出Viewcontroller須要實現的代理
@implementation BaseAdapter
-(instancetype)init{
	self = [super init];
	if (self){
		_dataArray = [[NSArray alloc]init];
	}
	return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{
	return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString * showUserInfoCellIdentifier = @"userInfoCell";
	UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
	if (cell == nil){
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
									  reuseIdentifier:showUserInfoCellIdentifier];
	}
	cell.textLabel.text = @"Default";
	cell.detailTextLabel.text = @"Default.detail";
	return cell;
}
@end

單獨目標實現:htm

#import <Foundation/Foundation.h>
#import "BaseAdapter.h"
@interface UserAdapter : BaseAdapter

@end

#import "UserAdapter.h"
#import "UserModer.h"
@implementation UserAdapter
-(instancetype)init{
	self = [super init];
	if (self){
		UserModer *moder = [[UserModer alloc]init:@"Fight" sub:@"iOS"];
		self.dataArray = @[moder,moder,moder,moder,moder,moder,moder,moder];
	}
	return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	return [super numberOfSectionsInTableView:tableView];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString * showUserInfoCellIdentifier = @"userInfoCell";
	UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
	if (cell == nil){
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
									  reuseIdentifier:showUserInfoCellIdentifier];
	}
	UserModer *moder = self.dataArray[indexPath.row];
	cell.textLabel.text = moder.Title;
	cell.detailTextLabel.text = moder.subTitle;
	return cell;
}
@end

被適配者(數據):對象

@interface UserModer : NSObject
@property (nonatomic , strong)NSString *Title;
@property (nonatomic , strong)NSString *subTitle;
-(instancetype)init:(NSString *)title sub:(NSString*) subTitle;
@end

#import "UserModer.h"

@implementation UserModer
-(instancetype)init:(NSString *)title sub:(NSString*) subTitle{
	self = [super init];
	if (self){
		self.Title = title;
		self.subTitle = subTitle;
	}
	return self;
}

@end

適配器參考:http://ibloodline.com/articles/2016/09/20/Adapter.htmlblog

相關文章
相關標籤/搜索