中介者模式

基本理解

  • 中介者模式又叫做调停者模式,其实就是中间人或者调停者的意思。
  • 尽管将一个系统分割成许多对象通常可以增加可复用性,但是对象之间的连接又降低了可复用性。
  • 如果两个类不必彼此直接通信,那么着两个类就不应当发生直接的相互作用。如果其中一个类需要调用另一个类的方法的话,可以用过第三者转发这个调用。而这个第三者就是中介者。
  • 概念:中介者模式(Mediator),用一个中介者对象来封装一系列的对象交互。中介者使各个对象不需要显式地相互引用,从而使其耦合松散,而且可 以独立地改变他们之间的交互。
  • UINavigationViewController就是属于一个中介者。

中介者模式的优缺点

中介者模式很容易在系统中应用,也很容易在系统中误用。当系统出现了多对多交互复杂的对象群时,不要急于使用中介者模式,而要先反思你在系统上设计是否合理。

优点就是集中控制,减少了对象之间的耦合度。缺点就是太过于集中。

应用场景

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

例子

CoordinatingViewController.h

//
// CoordinatingViewController.h
// CoordinateDemo
//
// Created by zhanggui on 15/8/6.
// Copyright (c) 2015年 zhanggui. All rights reserved.
// #import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger,ButtonTag) {
kButtontagOpenThree,
kButtontagOpenThreeNext,
kButtontagBackThree
};
@interface CoordinatingViewController : UIViewController @property(nonatomic,strong)NSMutableArray *controllersArray; @property(nonatomic,strong)UIViewController *activeController; @property(nonatomic,strong)UIViewController *mainViewController;
+(instancetype)shareInstance; - (void)requestViewChangeByObject:(id)sender;
@end

CoordinationgViewController.m

//
// CoordinatingViewController.m
// CoordinateDemo
//
// Created by zhanggui on 15/8/6.
// Copyright (c) 2015年 zhanggui. All rights reserved.
// #import "CoordinatingViewController.h"
#import "ThirdViewController.h"
#import "Third2ViewController.h" @interface CoordinatingViewController ()
{
// UIStoryboard *storyboard;
}
@end @implementation CoordinatingViewController +(instancetype)shareInstance
{
static CoordinatingViewController *coorVC;
if (coorVC==nil) {
coorVC = [[self alloc] init];
}
return coorVC;
}
- (void)viewDidLoad {
[super viewDidLoad];
// storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// Do any additional setup after loading the view.
}
- (void)requestViewChangeByObject:(id)sender {
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
if ([sender isKindOfClass:[UIButton class]]) {
switch ([sender tag]) {
case kButtontagOpenThree:
{
ThirdViewController *thirdVC = [storyboard instantiateViewControllerWithIdentifier:@"ThirdViewController"];
self.activeController = thirdVC;
[self.controllersArray addObject:thirdVC];
// UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:thirdVC];
[self.mainViewController presentViewController:thirdVC animated:YES completion:nil];
} break;
case kButtontagOpenThreeNext:
{
Third2ViewController *third2VC = [storyboard instantiateViewControllerWithIdentifier:@"Third2ViewController"];
[self.controllersArray addObject:third2VC];
UIViewController *cvc = [self.controllersArray objectAtIndex:1];
[cvc presentViewController:third2VC animated:YES completion:nil];
}
break;
case kButtontagBackThree:
{
UIViewController *cvc = [self.controllersArray objectAtIndex:2];
[cvc dismissViewControllerAnimated:YES completion:nil];
[self.controllersArray removeObjectAtIndex:2];
}
break;
default:
{
UIViewController *cvc = [self.controllersArray objectAtIndex:1];
[cvc dismissViewControllerAnimated:YES completion:nil];
[self.controllersArray removeObjectAtIndex:1];
}
break;
}
}
}
@end

上面这个就是中介类。

在ViewController.m

//
// ViewController.m
// CoordinateDemo
//
// Created by zhanggui on 15/8/6.
// Copyright (c) 2015年 zhanggui. All rights reserved.
// #import "ViewController.h"
#import "CoordinatingViewController.h" @interface ViewController ()
{
CoordinatingViewController *coorController;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
coorController = [CoordinatingViewController shareInstance];
coorController.controllersArray = [[NSMutableArray alloc] initWithObjects:self, nil];
_firstButton.tag = kButtontagOpenThree;
coorController.activeController = self;
coorController.mainViewController = self;
// Do any additional setup after loading the view, typically from a nib.
} - (IBAction)showAction:(id)sender {
[coorController requestViewChangeByObject:_firstButton];
}
@end

ThirdViewController.m

//
// ThirdViewController.m
// CoordinateDemo
//
// Created by zhanggui on 15/8/6.
// Copyright (c) 2015年 zhanggui. All rights reserved.
// #import "ThirdViewController.h"
#import "CoordinatingViewController.h"
@interface ThirdViewController () @end @implementation ThirdViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (IBAction)forwardAction:(id)sender {
if ([sender isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton *)sender;
btn.tag = kButtontagOpenThreeNext;
CoordinatingViewController *coor = [CoordinatingViewController shareInstance];
[coor requestViewChangeByObject:btn];
} }
@end

Third2ViewController.m

//
// Third2ViewController.m
// CoordinateDemo
//
// Created by zhanggui on 15/8/6.
// Copyright (c) 2015年 zhanggui. All rights reserved.
// #import "Third2ViewController.h"
#import "CoordinatingViewController.h"
@interface Third2ViewController () @end @implementation Third2ViewController - (void)viewDidLoad {
[super viewDidLoad]; // Do any additional setup after loading the view.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ - (IBAction)backAction:(id)sender {
if ([sender isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton *)sender;
btn.tag = kButtontagBackThree;
CoordinatingViewController *coor = [CoordinatingViewController shareInstance];
[coor requestViewChangeByObject:btn];
} } - (IBAction)forwardAction:(id)sender {
}
@end

上面的这两个就是视图迁移的中介处理ThirdViewController和Third2ViewController。

附:

最新文章

  1. 单点登录实现----CAS(一)
  2. core的生成
  3. OCS 开放缓存服务
  4. mybatis oracle java.sql.SQLException: 流已被关闭问题
  5. Shiro源码分析-初始化-Realm
  6. MVC项目总结(别人的好文章)
  7. oracle表分析
  8. python学习-day3
  9. jquery滚动到指定元素,模仿锚点
  10. conky 1.10以后的新配置格式
  11. Java 9 揭秘(1.Java入门介绍)
  12. 游戏AI(三)—行为树优化之基于事件的行为树
  13. browser-sync第一次打开提示路径错误,path.js应该输出字符串;之后重启一直提示插入代码片段,插入后无效依然提示
  14. leetcode152
  15. java多线程关键字volatile、lock、synchronized
  16. BZOJ1143 [CTSC2008]祭祀river 二分图匹配 最小链覆盖
  17. Linux软件安装中RPM与YUM 区别和联系
  18. mysql数据库索引相关
  19. 微软SMB 3.0文件共享协议新特性介绍
  20. mediawiki 安装 部署 配置 使用学习

热门文章

  1. RESTful API 设计指南【转】
  2. C#/C++ 中字节数组与int类型转换
  3. 关于FileStream读取大文件问题
  4. Android之TextView的Span样式源码剖析
  5. 受限玻尔兹曼机(RBM)学习笔记(七)RBM 训练算法
  6. vertical-align两种应用场合
  7. 【转】http-equiv=&quot;X-UA-Compatible&quot; 设置IE浏览器兼容模式详解
  8. Import 元素 (MSBuild)
  9. 【转】Aspose.Cells读取excel文件
  10. Jquery 前端模版