互相耦合 delegate notification
eg:经理有一个任务要交给员工完成
需要Manager和Employee两个类,而且都是使用单例
1.互相耦合---最差的耦合方式
在这种情况下,经理和员工互相知道对方的存在
Manager.h
@interface Manager : NSObject
+ (instancetype)shareInstance;
- (void)beginPrintTask;//开始任务,
- (void)celebratePrintDone;//结束任务
@end
Manager.m
@implementation Manager
+ (instancetype)shareInstance
{
static Manager * instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [Manager new];
});
return instance;
}
- (void)beginPrintTask{
[[Employee shareInstance] doPrintJob];//告诉员工要去做任务
}
- (void)celebratePrintDone{
NSLog(@"celebrate print done");
}
@end
Employee.h
@interface Employee : NSObject
+ (instancetype)shareInstance;
- (void)doPrintJob;
@end
Employee.m
@implementation Employee
+ (instancetype)shareInstance
{
static Employee * instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [Employee new];
});
return instance;
}
- (void)doPrintJob{
NSLog(@"doing printing job");
[[Manager shareInstance] celebratePrintDone];//告诉经理任务完成
}
@end
在VIewController里面直接调用
[[Manager shareInstance] beginPrintTask];
delegate
使用delegate可以让员工不知道经理的存在,但经理知道员工的存在,员工需要一个协议
Manager.h
@interface Manager : NSObject
+ (instancetype)shareInstance;
- (void)beginPrintTask;//开始任务,
- (void)celebratePrintDone;//结束任务
@end
Manager.m
+ (instancetype)shareInstance
{
static Manager * instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [Manager new];
});
return instance;
}
- (void)beginPrintTask{
[Employee shareInstance].delegate = self;
[[Employee shareInstance] doPrintJob];//告诉员工要去做任务
}
- (void)celebratePrintDone{
NSLog(@"celebrate print done");
}
Employee.h
@protocol EmployeePrintDelegate <NSObject>
- (void)printJobDone;
@end
@interface Employee : NSObject
+ (instancetype)shareInstance;
- (void)doPrintJob;
@property (nonatomic, weak) id<EmployeePrintDelegate> delegate;
@end
Employee.m
@implementation Employee
+ (instancetype)shareInstance
{
static Employee * instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [Employee new];
});
return instance;
}
- (void)doPrintJob{
NSLog(@"doing printing job");
if (_delegate) {
[_delegate printJobDone];
}
}
@end
notification
这种情况下,经理和员工互相不知道对方的存在
Manager.h
@interface Manager : NSObject
+ (instancetype)shareInstance;
- (void)beginPrintTask;//开始任务,
- (void)celebratePrintDone;//结束任务
@end
Manager.m
+ (instancetype)shareInstance
{
static Manager * instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [Manager new];
});
return instance;
}
//监听通知
- (instancetype)init{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(celebratePrintDone) name:Notif_PrintTaskDone object:nil];
}
return self;
}
- (void)beginPrintTask{
[[NSNotificationCenter defaultCenter] postNotificationName:Notif_beginPrintTask object:nil];
}
- (void)celebratePrintDone{
NSLog(@"celebrate print done");
}
Employee.h
@interface Employee : NSObject
+ (instancetype)shareInstance;
- (void)doPrintJob;
@end
Employee.m
@implementation Employee
+ (instancetype)shareInstance
{
static Employee * instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [Employee new];
});
return instance;
}
//监听通知
- (instancetype)init{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doPrintJob) name:Notif_beginPrintTask object:nil];
}
return self;
}
- (void)doPrintJob{
NSLog(@"doing printing job");
[[NSNotificationCenter defaultCenter] postNotificationName:Notif_PrintTaskDone object:nil];
}
@end
再加上一个.h文件
#define Notif_beginPrintTask @"Notif_beginPrintTask"
#define Notif_PrintTaskDone @"Notif_PrintTaskDone"