参考:
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
作用:
- 持有具体事件类
- 实现具体事件处理逻辑
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
作用:
- 声明收到消息 抽象方法
- 实现发送消息方法与构造方法
注意:
- 为了防止循环引用, 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大神给我带来的很多帮助与启发。