六大设计原则
单一职责
一个类则负责一件事。UIView只负责事件传递和响应。CALayer 视图展示
开闭原则
对修改关闭,对扩展开放
接口隔离
使用多个专门的协议,而不是一个庞大臃肿的协议。协议中的方法应尽量的少。eg. UITableViewDataSource 、UITableViewDelegate
依赖倒置原则
抽象不应该依赖于具体的实现,具体实现可以依赖抽象。eg. 上层只知道增删改查,不用知道底层用的是那种数据存储方案
里氏替换原则
父类可以被子类无缝替换,且原有功能不受影响 eg.KVO
迪米特法则
一个对象应当对其他对象有尽可能的少的了解。 高内聚、低耦合
五大设计模式
责任链
概念:为请求创建了一个对象响应者链。关键是:响应链。
场景:业务处理顺序A --> B --> C 变更为 C --> B --> A
代码实现
@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 处理
桥接模式
概念:抽象化与实现化解耦,使得二者可以独立变化
场景:列表根据三种不同数据源,显示三种不同的样式
解除列表和数据的耦合: ClassA带表列表抽象,ClassB代表数据数据抽象。ClassA拥有一个ClassB的实例对象。A1 B1等是具体实现。
代码实现
@interface BaseView : UIView
//桥接模式核心实现
@property(nonatomic,strong)BaseData *data;
//获取数据
-(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中,可以随意组合列表和数据
BaseView *view = [[View1 alloc]init];
view.data = [[Data2 alloc]init];
[view handle];
适配器
概念:
对象适配器: 新的类包含一个旧的类的实例对象, 引入新的逻辑。将一个类的接口转换成客户希望的另外一个接口
类适配器:继承,通过子类引入新的逻辑
场景:旧的类使用好几年了没有变动。但是需求边了。要加入一些新的东西
@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
单例模式
概念:该类负责创建自己的对象,同时确保只有单个对象被创建
+(instancetype)sharedInstance
{
static User *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[super allocWithZone:NULL]init]; //注意点
});
return instance;
}
//必不可少
+(id)allocWithZone:(struct _NSZone *)zone
{
return [self sharedInstance];
}
//必不可少
-(id)copy
{
return self;
}
命令模式
概念:行为参数化,降低代码重合度。
场景:将一个请求封装成一个对象,并把命令传给响应的对象。eg .点赞,转发等这些常见需求
代码实现
@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
+(instancetype)sharedInstance;
//执行命令
+(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;
}
//执行命令
+(void)executeCommand:(Command *)cmd completion:(CommandCompletionCallBack)completion
{
if(cmd){
//命令正在执行,则不处理
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