[架构]中介者模式

本人所有文章目录:http://my.oschina.net/ChenTF/blog/677112

 

参考:

C#方式实现:  http://blog.csdn.net/fly_yr/article/details/8576130

OC实现(此实现方式存在循环引用问题): http://my.oschina.net/daguoshi/blog/499906

 

介绍


     用一个对象来封装一系列对象的交互方式。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

使用场景:

  •      对象间的交互虽定义明确然而非常复杂,导致一组对象彼此依赖而且难以理解
  •      因为对象引用了许多其他对象并与其通讯,导致对象难以复用。
  •      想要定制一个分布在多个类中的逻辑或行为,又不想生成太多的子

类图


 

实现


1.抽象中介者(Mediator)

#import <Foundation/Foundation.h>
@class Colleague;

/**
 *  抽象中介者
 */
@interface Mediator : NSObject

- (void)sendWithMess:(NSString *)message Colleague:(Colleague *)colleague;


@end

作用: 1.定义抽象方法

 

2.具体中介者(ConcreteMediator)

#import "Mediator.h"

#import "ConcreteColleague1.h"
#import "ConcreteColleague2.h"

/**
 *  具体中介者类
 */
@interface ConcreteMediator : Mediator

@property (nonatomic, strong) ConcreteColleague1 * colleague1;
@property (nonatomic, strong) ConcreteColleague2 * colleague2;

@end
#import "ConcreteMediator.h"

@implementation ConcreteMediator

- (void)sendWithMess:(NSString *)message Colleague:(Colleague *)colleague {
    
    if (colleague == self.colleague1) {
        
        [self.colleague2 notifyWithMess:message];
    }
    
    if (colleague == self.colleague2) {
        
        [self.colleague1 notifyWithMess:message];
    }
}

#pragma mark - Getters & Setters
- (ConcreteColleague1 *)colleague1 {
    if (_colleague1 == nil) {
        _colleague1 = [[ConcreteColleague1 alloc] initWithMediator:self];
    }
    return _colleague1;
}

- (ConcreteColleague2 *)colleague2 {
    if (_colleague2 == nil) {
        _colleague2 = [[ConcreteColleague2 alloc] initWithMediator:self];
    }
    return _colleague2;
}

@end

作用:

  1. 持有具体事件类  
  2. 实现具体事件处理逻辑

 

3.抽象同事类

#import <Foundation/Foundation.h>

#import "Mediator.h"

/**
 *  同事类的协议
 */
@protocol Colleague <NSObject>

@required
- (void)notifyWithMess:(NSString *)mess;

- (void)sendWithMess:(NSString *)mess;
@end

/**
 *  抽象同事类
 */
@interface Colleague : NSObject<Colleague>

@property (nonatomic, assign) Mediator *mediator;

- (instancetype)initWithMediator:(Mediator *)mediator;

@end
#import "Colleague.h"

@implementation Colleague

- (instancetype)initWithMediator:(Mediator *)mediator {
    self = [super init];
    if (self) {
        self.mediator = mediator;
    }
    
    return self;
}


#pragma mark - Colleague
- (void)notifyWithMess:(NSString *)mess {
    
}

- (void)sendWithMess:(NSString *)mess {
    
    [self.mediator sendWithMess:mess Colleague:self];
}

@end

作用:

  1. 声明收到消息 抽象方法  
  2. 实现发送消息方法与构造方法

注意:

  • 为了防止循环引用, mediator是weak修饰

 

4.具体同事类

#import "Colleague.h"

/**
 *  具体同事类
 */
@interface ConcreteColleague1 : Colleague

@end


#import "ConcreteColleague1.h"

@implementation ConcreteColleague1

- (void)notifyWithMess:(NSString *)mess {
    
    NSLog(@"同事1 收到消息:%@", mess);
}

- (void)dealloc {
    
}

@end

作用: 1.实现收到消息的处理

 

5.调用

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.mediator = [[ConcreteMediator alloc] init];
    
    [self.mediator.colleague1 sendWithMess:@"1的消息, 吃了吗?"];
    [self.mediator.colleague2 sendWithMess:@"2的消息, 还没,要请客吗?"];
}

结果:

 

Code:


https://github.com/ChenTF/MediatorPatterDemo

 

更好的实现方式


    本文是使用的传统版中介者模式, 但是在iOS下可以运用运行 来更优雅的实现, 推荐Casa大神的这篇文章(http://casatwy.com/iOS-Modulization.html), 再次感谢Casa大神给我带来的很多帮助与启发。

转载于:https://my.oschina.net/ChenTF/blog/677088

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值