GoF23种设计模式之行为型模式之中介者模式
来源:互联网 发布:伏尼契手稿知乎 编辑:程序博客网 时间:2024/05/21 03:55
一、概述
使用一个中介对象来封装一系列的对象交互。中介者让各个对象无需显式地相互引用,从而达到解耦的效果。并且可以独立地改变它们之间的交互。
二、适用性
1.当一组对象以定义良好但复杂通信的时候。产生的相互依赖关系结构混乱且难以理解。
2.当一个对象引用其他许多对象并且直接与这些对象通信的时候。会导致难以复用该对象。
3.当需要定制一个分布在多个类中的行为,而又不想生成过多的子类的时候。
三、参与者
1.Mediator:中介者定义一个接口用于与各个同事(Colleague)对象通信。
2.ConcreteMediator:具体中介者通过协调各同事对象实现协作行为。了解并维护它的各个同事。
3.Colleagueclass:每一个同事类都知道它的中介者对象。每一个同事对象在需与其他的同事通信的时候,与它的中介者通信
四、类图
五、示例
Mediator
package cn.lynn.mediator;public abstract class Mediator { public abstract void notice(String content);}ConcreteMediator
package cn.lynn.mediator;public class ConcreteMediator extends Mediator { private Front front; private Engineer engineer; public ConcreteMediator() { front = new Front(); engineer = new Engineer(); } @Override public void notice(String content) { if (content.equals("boss")) { engineer.action(); } else if (content.equals("client")) { front.action(); } }}Colleagueclass
package cn.lynn.mediator;public abstract class Employee { public abstract void action();}
package cn.lynn.mediator;public class Engineer extends Employee { @Override public void action() { System.out.println("工程师努力工作!"); }}
package cn.lynn.mediator;public class Front extends Employee { @Override public void action() { System.out.println("前台接待客人!"); }}Client
package cn.lynn.mediator;public class Client { public static void main(String[] args) { Mediator mediator = new ConcreteMediator(); mediator.notice("boss"); // 老板来了,赶紧的 mediator.notice("client"); // 客户到访, 接待啊 }}Result
工程师努力工作!前台接待客人!
- GoF23种设计模式之行为型模式之中介者模式
- 设计模式之------行为型模式(一)-----中介者模式
- GOF23设计模式之中介者模式的理解
- GOF23设计模式之中介者模式的实现
- GOF23设计模式之 中介者模式Mediator
- 设计模式笔记--行为型模式之五--中介者
- java设计模式(行为型)之中介者模式
- 设计模式(行为型)之中介者模式(Mediator Pattern)
- 设计模式-行为型之中介者模式
- 行为型:设计模式之中介者模式(十四)
- Java设计模式(行为型)之-中介者模式
- 行为型模式之中介者模式
- 二十三种设计模式之行为型模式之中介者模式
- GoF23种设计模式之行为型模式之迭代器模式
- GoF23种设计模式之行为型模式之备忘录模式
- GoF23种设计模式之行为型模式之观察者模式
- GoF23种设计模式之行为型模式之状态模式
- GoF23种设计模式之行为型模式之责任链模式
- Application "XXX" could not be found in the registry. The applications available are: .
- ireport自定义行数分页
- gitolite建立git服务器
- 九度oj1001
- win7 假死
- GoF23种设计模式之行为型模式之中介者模式
- 如何在安装双启动后卸载 Windows 或者 Linux
- Red Hat AS 5.5(64Bit)安装Oracle(10g-补丁10.2.0.5)
- CSharp OOP - Polymorphism (Compile Time, Run Time, Parametric)
- mysql导入excel数据
- qt学习笔记(转)
- GoF23种设计模式之行为型模式之备忘录模式
- 新人入职引导的思考
- js去除字符串最后