六、中介者模式Mediator(行为型模式)
来源:互联网 发布:网络钟点工陪人聊天 编辑:程序博客网 时间:2024/06/05 07:12
简述
调停者模式(Mediator pattern)软件设计模式的一种,用于模块间解耦,通过避免对象互相显式的指向对方从而降低耦合。
整合核心于决策核心
主板式汇集众多接口的整合体,其中最特殊的接口是它于CPU之间的接口,如图:
CPU本身包容了非常复杂的运算逻辑,而MB又通过接口将它包容进来,展现MB的极大包容力,而且至今MB的包容力仍然日益增强钟。
这种结构并不局限于硬件上,软件上也很常见:
Mediator=整合核心+决策核心
问题
① 多个对象重复访问,无规律性。
② 一个对象集合的交互良好定义,但是交互方式复杂。通常有非结构依赖。
③ 对象复用困难,因为对象指向和交互很多其他的对象
④ 在多个类之间的分布式行为的控制问题:应该不用建立很多子类就是先本地化
参与者说明
① Mediator: 定义了与colleague对象交互的接口,通常被告知事件或者状态
② concreteMediator:实现与colleague交互的具体行为,了解和维护colleagues
③ colleague: 每个colleague知道自己的Mediator对象,每个colleague当其需要与其他colleague交互时需要先与自己的mediator进行交互,为mediator提供了服务,colleague可能请求相同也可能不同
结果
① 定义对象,其封装了一组对象的交互方式
② Mediator促进低耦合,通过纺织对象显示的引用彼此;
③ 让人独立的拓展其交互 集中式控制
Colleague抽象类别定义了mediator指针可以参考到Mediator类别体系的对象。而ConcreteColleague1、ConcreteColleague2等类别都继承了这mediator指针,因此ConcreteColleague1、ConcreteColleague2等类别的对象可以接着mediator指针而传送讯息给Mediator的对象。
应用
一、在Android开发中Activity一般就适合担任协调者的角色,如图:
ac01扮演了整个View间的互动与合作的协调者。
二、Android的Binder Kernel:
Demo
未运用中介模式前的实现:
abstract class Person { private String name; private int condition; public Person(String name, int condition) { this.name = name; this.condition = condition; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCondition() { return condition; } public void setCondition(int condition) { this.condition = condition; } public abstract void getPartner(Person person);}
class Man extends Person { public Man(String name, int condition) { super(name, condition); } public void getPartner(Person person) { if (person instanceof Man) { System.out.println("汗,我不是同性恋!"); } else { if (this.getCondition() == person.getCondition()) { System.out.println(this.getName() + "和" + person.getName() + "绝配"); } else { System.out.println(this.getName() + "和" + person.getName() + "不相配"); } } }}
class Woman extends Person { public Woman(String name, int condition) { super(name, condition); } public void getPartner(Person person) { if (person instanceof Woman) { System.out.println("汗,我不是同性恋!"); } else { if (this.getCondition() == person.getCondition()) { System.out.println(this.getName() + "和" + person.getName() + "绝配"); } else { System.out.println(this.getName() + "和" + person.getName() + "不相配"); } } }}
public class Test { public static void main(String[] args) { Person zhangsan = new Man("张三", 5); Person lisi = new Man("李四", 6); Person xiaofang = new Woman("小芳", 6); zhangsan.getPartner(xiaofang); lisi.getPartner(xiaofang); zhangsan.getPartner(lisi); /*张三和小芳不相配 李四和小芳绝配 汗,我不是同性恋!*/ }}运用了中介模式的代码:
class Mediator { private Man man; private Woman woman; public void setMan(Man man) { this.man = man; } public void setWoman(Woman woman) { this.woman = woman; } public void getPartner(Person person) { // 将搭档设置上 if (person instanceof Man) { this.setMan((Man) person); } else { this.setWoman((Woman) person); } // 判断条件 if (man == null || woman == null) { System.out.println("汗,我不是同性恋!"); } else { if (man.getCondition() == woman.getCondition()) { System.out.println(man.getName() + "和" + woman.getName() + "绝配"); } else { System.out.println(man.getName() + "和" + woman.getName()+ "不相配"); } } }}
abstract class Person { private String name; private int condition; private Mediator mediator; public Person(String name, int condition, Mediator mediator) { super(); this.name = name; this.condition = condition; this.mediator = mediator; } public Mediator getMediator() { return mediator; } public void setMediator(Mediator mediator) { this.mediator = mediator; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCondition() { return condition; } public void setCondition(int condition) { this.condition = condition; } public abstract void getPartner(Person person);}
class Man extends Person { public Man(String name, int condition, Mediator mediator) { super(name, condition, mediator); } public void getPartner(Person person) { this.getMediator().setMan(this); this.getMediator().getPartner(person); }}class Woman extends Person { public Woman(String name, int condition, Mediator mediator) { super(name, condition, mediator); } public void getPartner(Person person) { this.getMediator().setWoman(this); this.getMediator().getPartner(person); }}
public class Test { public static void main(String[] args) { Mediator mediator = new Mediator(); Person zhangsan = new Man("张三", 7, mediator); Person lisi = new Man("李四", 7, mediator); Person xiaofang = new Woman("小芳", 7, mediator); zhangsan.getPartner(xiaofang); lisi.getPartner(xiaofang); zhangsan.getPartner(lisi); /* * 张三和小芳绝配 李四和小芳绝配 李四和小芳绝配 */ }}
0 0
- 六、中介者模式Mediator(行为型模式)
- 行为型-中介者模式(Mediator)
- 行为型模式-Mediator ( 中介者模式 )
- 中介者模式(Mediator)(对象行为型模式)
- 行为型模式-----中介者模式(Mediator)
- 设计模式--中介者模式Mediator(行为型)
- 设计模式-行为型-中介者模式(Mediator)
- 行为型模式之中介者模式(Mediator)
- Mediator中介者(行为型设计模式)
- 行为型模式-中介者mediator
- 行为型模式之中介者MEDIATOR
- 行为型模式-中介者(mediator)
- 行为模式 - 中介者(Mediator)
- 行为模式: Mediator(中介者)
- 中介者(Mediator)----对象行为型模式
- 设计模式--行为型-Mediator(中介者)
- 3.5 Mediator(中介者) -- 对象行为型模式
- 十七、中介者模式Mediator(行为型)
- poj 2318 TOYS (叉积)
- eclipse插件打包
- 让ASP.NET MVC页面返回不同类型的内容
- Android-6.0-23的getColor()过时替代方法
- 关于LintCode。
- 六、中介者模式Mediator(行为型模式)
- svn提交报org.apache.subversion.javahl.ClientException: Filesystem has no item
- 游戏服务器数据库踩过的坑
- ios开发多线程篇——创建线程
- 游戏服务器,每秒需要处理百来次数据库的读写操作,如何设计比较好?
- Selenium - Why there is no need of external firefox driver like IE and chrome in selenium2.0?
- C++ 类的static成员
- CSS有4种定义方式--类+ ID+ 标签+ 伪类
- 音视频开发需要的一些工具总结