美文网首页
iOS架构之耦合

iOS架构之耦合

作者: 心底碎片 | 来源:发表于2017-02-22 16:20 被阅读52次

互相耦合 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"

相关文章

  • 基于彻底解耦合的实验性iOS架构

    基于彻底解耦合的实验性iOS架构 基于彻底解耦合的实验性iOS架构

  • iOS架构之耦合

    互相耦合 delegate notification eg:经理有一个任务要交给员工完成需要Manager和E...

  • 应用架构文章

    Service Oriented 的 iOS 应用架构 新浪微博iOS客户端架构与优化之路 糯米移动组件架构演进之...

  • iOS架构

    这里说几个概念:iOS系统框架:iOS系统架构:iOS架构:iOS 代码架构:iOS架构师:iOS架构设计: iO...

  • iOS有关架构组件化的文章链接

    iOS应用架构谈 组件化方案 iOS 组件化方案探索 iOS移动端架构的那些事 如何优雅的实现界面跳转 之 统跳协...

  • [iOS] 架构之 View

    参考文章 iOS架构之View层的架构方案 OS应用架构谈 view层的组织和调用方案

  • iOS架构

    iOS架构iOS架构

  • 交友2.0总结

    新架构尝试在交友中尝试使用新架构,原因在于原有的架构耦合度较高,希望能够优化层次结构,降低耦合度,提高项目可维护性...

  • 玩转iOS开发:打造一个低耦合可复用的《TableViewCon

    玩转iOS开发:打造一个低耦合可复用的《TableViewController》 玩转iOS开发:打造一个低耦合可...

  • iOS 从0到1搭建高可用App框架

    最近更新——iOS 从0到1搭建高可用App框架(二) 1. 如何运用“高内聚,低耦合”的架构思想设计应用 2. ...

网友评论

      本文标题:iOS架构之耦合

      本文链接:https://www.haomeiwen.com/subject/xbdmwttx.html