iOS架构之耦合

互相耦合 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"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • iOS网络架构讨论梳理整理中。。。 其实如果没有APIManager这一层是没法使用delegate的,毕竟多个单...
    yhtang阅读 10,680评论 1 23
  • iOS应用架构谈 view层的组织和调用方案 iOS应用架构谈 开篇 iOS应用架构谈 view层的组织和调用方案...
    其实也没有阅读 5,824评论 0 15
  • 我用数千年历史面貌的恢宏演变,信手拈来,叙述一个砾石的真实故事,权当茶余饭后消遣之、聊以增一笑耳。 我是砾石,来自...
    晚成寄语阅读 4,183评论 2 8
  • 今天我们有一节体育课,这节体育课老师叫了几个男生当中也有我。老师让我们出去练反跳的跳绳比赛,结果我才跳了一个。别人...
    子细阅读 1,711评论 0 0
  • 昨晚没睡好,今天还又忙了一天,此刻两只眼睛已经充满红血丝,困到眼皮打架,真心不行了,我要睡觉了,想念我的枕头,一切...
    happyMia阅读 1,045评论 0 0

友情链接更多精彩内容