iOS设计模式之中介者模式
中介者模式
基本理解
- 中介者模式又叫做调停者模式,其实就是中间人或者调停者的意思。
- 尽管将一个系统分割成许多对象通常可以增加可复用性,但是对象之间的连接又降低了可复用性。
- 如果两个类不必彼此直接通信,那么着两个类就不应当发生直接的相互作用。如果其中一个类需要调用另一个类的方法的话,可以用过第三者转发这个调用。而这个第三者就是中介者。
- 概念:中介者模式(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。
附:
iOS设计模式之中介者模式的更多相关文章
- IOS设计模式之三:MVC模式
IOS设计模式之三:MVC模式 模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...
- 折腾Java设计模式之中介者模式
博文原址:折腾Java设计模式之中介者模式 中介者模式 中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性.这种模式提供了一个中介类,该类通常处理不同类之间的通信,并 ...
- iOS设计模式 - (3)简单工厂模式
iOS设计模式 - (3)简单工厂模式 by Colin丶 转载请注明出处: http://blog.csdn.net/hitwhylz/article/ ...
- js设计模式——8.中介者模式
js设计模式——8.中介者模式 /*js设计模式——中介者模式*/ class A { constructor() { this.number = 0; } setNumber(num, m) { t ...
- IOS设计模式之一(MVC模式,单例模式)
iOS 设计模式-你可能已经听说过这个词,但是你真正理解它意味着什么吗?虽然大多数的开发者可能都会认为设计模式是非常重要的,然而关于设计模式这一主题的文章却不多,并且有时候我们开发者在写代码的时候也不 ...
- 【GOF23设计模式】中介者模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_中介者模式.同事协作类.内部类实现 package com.test.mediator; /** * 同事类的接口 */ ...
- [设计模式] 17 中介者模式 Mediator Pattern
在GOF的<设计模式:可复用面向对象软件的基础>一书中对中介者模式是这样说的:用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变 ...
- 再起航,我的学习笔记之JavaScript设计模式23(中介者模式)
中介者模式 概念介绍 中介者模式(Mediator):通过中介者对象封装一系列对象之间的交互,使对象之间不再相互引用降低他们之间的耦合,有时中介者对象也可以改变对象之间的交互. 创建一个中介 中介者模 ...
随机推荐
- ThinkJS 项目用 WebStorm 来设置断点与调试
1. 前置条件.已按ThinkJS 2.0 文档 之 <创建项目> 建好项目. 说明a: 本示例创建项目名为wagang,使用es6配置: thinkjs new wagang --es6 ...
- Flex Error #2156问题
出现这个问题是因为应用程序使用了特殊端口,修改端口就可以解决. 特殊端口列表 A security change has been made in Adobe Flash Player 9.0.115 ...
- 【原创】C#搭建足球赛事资料库与预测平台(4) 比赛信息数据表设计
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源C#彩票数据资料库系列文章总目录:[目录]C#搭建足球赛事资料库与预测平台与彩票数据分析目录 本篇文章开始将逐步介 ...
- 使用Service.Stack客户端编写redis pub sub的方法
pub相对简单 client.PublishMessage("channel", "msg"); sub有2种方法 方法1 var subscription ...
- [Python] Create a Django project in Pycharm
From: http://blog.csdn.net/u013088062/article/details/50158239 From: http://blog.csdn.net/u013088062 ...
- Linux bash - 常用操作命令
一.终端基础 本文摘录一些本人在学习Linux(CentOS 6.6) bash命令,并且会不定期保持更新. 在此先介绍一下Linux shell终端的常规命令输入格式,如下图: 上图中root是用户 ...
- Sprint2演示分
团队贡献分: 朱杰:22 蔡京航:21 华子仪:20 甄增文:17
- 准备.Net转前端开发-WPF界面框架那些事,值得珍藏的8个问题
题外话 不出意外,本片内容应该是最后一篇关于.Net技术的博客,做.Net的伙伴们忽喷忽喷..Net挺好的,微软最近在跨平台方面搞的水深火热,更新也比较频繁,而且博客园的很多大牛也写的有跨平台相关技术 ...
- MIUI选项框开关样式模拟
有IOS的开关模拟,当然也有MIUI的开关模拟 看到设置选项里面的开关样式,突发奇想地来试试 最终效果如图: 实现过程 1. 选项框checkbox 模拟开关当然需要一个选项框,这里用到了复选框 ...
- 重构第21天 合并继承 (Collapse Hierarchy)
理解:本文中的”合并继承”是指如果子类的属性和方法也适合于基类,那么就可以移除子类,从而减少依赖关系. 详解:上一篇我们讲到“提取子类”重构是指当基类中的一个责任不被所有的子类所需要时,将这些责任提取 ...