六大設(shè)計原則
單一職責(zé)
一個類則負(fù)責(zé)一件事淋样。UIView只負(fù)責(zé)事件傳遞和響應(yīng)胳螟。CALayer 視圖展示
開閉原則
對修改關(guān)閉,對擴(kuò)展開放
接口隔離
使用多個專門的協(xié)議接校,而不是一個龐大臃腫的協(xié)議猛频。協(xié)議中的方法應(yīng)盡量的少狮崩。eg. UITableViewDataSource 、UITableViewDelegate
依賴倒置原則
抽象不應(yīng)該依賴于具體的實現(xiàn)鹿寻,具體實現(xiàn)可以依賴抽象睦柴。eg. 上層只知道增刪改查,不用知道底層用的是那種數(shù)據(jù)存儲方案
里氏替換原則
父類可以被子類無縫替換毡熏,且原有功能不受影響 eg.KVO
迪米特法則
一個對象應(yīng)當(dāng)對其他對象有盡可能的少的了解坦敌。 高內(nèi)聚、低耦合
五大設(shè)計模式
責(zé)任鏈
概念:為請求創(chuàng)建了一個對象響應(yīng)者鏈痢法。關(guān)鍵是:響應(yīng)鏈狱窘。
場景:業(yè)務(wù)處理順序A --> B --> C 變更為 C --> B --> A
代碼實現(xiàn)
@class Business;
typedef void (^CompletionBlock)(BOOL handled);
typedef void (^ResultBlock)(Business *handler,BOOL handled);
@interface Business : NSObject
@property(nonatomic,strong)Business *nextBusiness;
@property(nonatomic,assign)int tag;
-(void)handle:(ResultBlock)result;
@end
@implementation Business
-(void)handle:(ResultBlock)result
{
[self handleBusiness:^(BOOL handled) {
if(handled){
result(self,handled);
}else{
if(self.nextBusiness){
[self.nextBusiness handle:result];
}else{
result(nil,NO);
}
}
}];
}
-(void)handleBusiness:(CompletionBlock)completion
{
if(_tag == 3){
NSLog(@"tag = %d 處理",_tag);
completion(YES);
}else{
NSLog(@"tag = %d 不處理",_tag);
completion(NO);
}
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
Business *a = [[Business alloc]init];
a.tag = 4;
Business *b = [[Business alloc]init];
b.tag = 5;
Business *c = [[Business alloc]init];
c.tag = 3;
Business *d = [[Business alloc]init];
d.tag = 1;
a.nextBusiness = b;
b.nextBusiness = c;
c.nextBusiness = d;
[a handle:^(Business *handler, BOOL handled) {
}];
}
2018-04-23 14:52:56.793052+0800 TestDemo[2965:170447] tag = 4 不處理
2018-04-23 14:52:56.793195+0800 TestDemo[2965:170447] tag = 5 不處理
2018-04-23 14:52:56.793326+0800 TestDemo[2965:170447] tag = 3 處理
橋接模式
概念:抽象化與實現(xiàn)化解耦,使得二者可以獨(dú)立變化
場景:列表根據(jù)三種不同數(shù)據(jù)源财搁,顯示三種不同的樣式
解除列表和數(shù)據(jù)的耦合: ClassA帶表列表抽象蘸炸,ClassB代表數(shù)據(jù)數(shù)據(jù)抽象。ClassA擁有一個ClassB的實例對象尖奔。A1 B1等是具體實現(xiàn)搭儒。
代碼實現(xiàn)
@interface BaseView : UIView
//橋接模式核心實現(xiàn)
@property(nonatomic,strong)BaseData *data;
//獲取數(shù)據(jù)
-(void)handle;
@end
@implementation BaseView
-(void)handle
{
[self.data fetchData];
}
@end
@interface BaseData : NSObject
-(void)fetchData;
@end
@implementation BaseData
-(void)fetchData
{
//ovveride to subClass
}
@end
//在ViewDidLoad中,可以隨意組合列表和數(shù)據(jù)
BaseView *view = [[View1 alloc]init];
view.data = [[Data2 alloc]init];
[view handle];
適配器
概念:
對象適配器: 新的類包含一個舊的類的實例對象提茁, 引入新的邏輯淹禾。將一個類的接口轉(zhuǎn)換成客戶希望的另外一個接口
類適配器:繼承,通過子類引入新的邏輯
場景:舊的類使用好幾年了沒有變動甘凭。但是需求邊了稀拐。要加入一些新的東西
@interface Target : NSObject
-(void)operation;
@end
@interface CoolTarget : NSObject
@property(nonatomic,strong)Target *target; //被適配對象
-(void)request;
@end
@implementation CoolTarget
-(void)request
{
//額外處理
[self.target operation];
//額外處理
}
@end
單例模式
概念:該類負(fù)責(zé)創(chuàng)建自己的對象,同時確保只有單個對象被創(chuàng)建
+(instancetype)sharedInstance
{
static User *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[super allocWithZone:NULL]init]; //注意點(diǎn)
});
return instance;
}
//必不可少
+(id)allocWithZone:(struct _NSZone *)zone
{
return [self sharedInstance];
}
//必不可少
-(id)copy
{
return self;
}
命令模式
概念:行為參數(shù)化丹弱,降低代碼重合度。
場景:將一個請求封裝成一個對象铲咨,并把命令傳給響應(yīng)的對象躲胳。eg .點(diǎn)贊,轉(zhuǎn)發(fā)等這些常見需求
代碼實現(xiàn)
@class Command;
typedef void (^CommandCompletionCallBack)(Command* cmd);
@interface Command : NSObject
@property(nonatomic,copy)CommandCompletionCallBack completion;
-(void)execute;
-(void)cancel;
-(void)done;
@end
@implementation Command
-(void)execute
{
//override to subClass
[self done];
}
-(void)cancel
{
self.completion = nil;
}
-(void)done
{
dispatch_async(dispatch_get_main_queue(), ^{
if(_completion){
_completion(self);
}
self.completion = nil;
});
}
@end
@interface CommandManager : NSObject
@property(nonatomic,strong)NSMutableArray<Command*> *arrayCommands;
+(instancetype)sharedInstance;
//執(zhí)行命令
+(void)executeCommand:(Command *)cmd completion:(CommandCompletionCallBack)completion;
//取消命令
+(void)cancelCommand:(Command *)cmd;
@end
@implementation CommandManager
+(instancetype)sharedInstance
{
static CommandManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[super allocWithZone:NULL]init];
});
return instance;
}
-(id)init
{
self = [super init];
if(self){
_arrayCommands = [NSMutableArray array];
}
return self;
}
//執(zhí)行命令
+(void)executeCommand:(Command *)cmd completion:(CommandCompletionCallBack)completion
{
if(cmd){
//命令正在執(zhí)行纤勒,則不處理
if(![self _isExecutingCommand:cmd]){
[[[CommandManager sharedInstance] arrayCommands] addObject:cmd];
cmd.completion = completion;
[cmd execute];
}
}
}
//取消命令
+(void)cancelCommand:(Command *)cmd
{
if(cmd){
[[[CommandManager sharedInstance] arrayCommands] removeObject:cmd];
[cmd cancel];
}
}
+(BOOL)_isExecutingCommand:(Command*)cmd
{
NSArray *cmds = [[CommandManager sharedInstance] arrayCommands];
for (Command *aCmd in cmds) {
if(cmd == aCmd){
return YES;
}
}
return NO;
}
@end