GOF设计模式之中介者模式(Mediator)与命令模式(Command)
中介者模式(Mediator)
定义一个中介对象来封装一系列对象之间的交互,使原有对象之间的耦合松散,且可以独立地改变它们之间的交互。中介者模式又叫调停模式,它是迪米特法则的典型应用。
职责接口
1 | public interface Mediator { |
部门接口
1 | //同事类接口 |
研发部
1 | public class Development implements Department{ |
财务部
1 | public class Finacial implements Department{ |
市场部
1 | public class Market implements Department{ |
总管理
1 | public class President implements Mediator{ |
测试
1 | public class Client { |
测试
调研接项目
市场部汇报工作
数钱
命令模式(Command)
将请求封装为一个对象,从而使我们可用不同的请求对客户端进行参数化。
执行者
1 | /** |
命令接口及实现
1 | /** |
调用
1 | public class Invoke { |
测试
1 | public class Client { |
结果
Receiver.action()