中介者模式
定义
Define an object that encapsulates how a set of objects interact.Mediator promotes loose coupling by keeping objects from referring to each other explicitly,and it lets you vary their interaction independently.
用一个中介对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
代码实现
中介者
抽象中介者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package org.example.mediator;public abstract class Mediator { protected ConcreteColleague1 concreteColleague1 ; protected ConcreteColleague2 concreteColleague2 ; public ConcreteColleague1 getConcreteColleague1 () { return this .concreteColleague1 ; } public void setConcreteColleague1 (ConcreteColleague1 concreteColleague1) { this .concreteColleague1 = concreteColleague1 ; } public ConcreteColleague2 getConcreteColleague2 () { return this .concreteColleague2 ; } public void setConcreteColleague2 (ConcreteColleague2 concreteColleague2) { this .concreteColleague2 = concreteColleague2 ; } public abstract void execute () ; public abstract void doSomething1 () ; public abstract void doSomething2 () ; }
具体中介者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package org.example.mediator;public class ConcreteMediator extends Mediator { @Override public void execute () { doSomething1(); doSomething2(); } @Override public void doSomething1 () { System.out.println("中介者处理的业务逻辑1" ); this .concreteColleague1.selfMethod1(); this .concreteColleague2.selfMethod1(); } @Override public void doSomething2 () { System.out.println("中介者处理的业务逻辑2" ); this .concreteColleague1.selfMethod1(); this .concreteColleague2.selfMethod1(); } }
同事角色
抽象同事角色
1 2 3 4 5 6 7 8 9 10 11 12 package org.example.mediator;public abstract class Colleague { protected Mediator mediator ; public Colleague (Mediator mediator) { this .mediator = mediator ; } }
具体同事角色1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package org.example.mediator;public class ConcreteColleague1 extends Colleague { public ConcreteColleague1 (Mediator mediator) { super (mediator); } public void selfMethod1 () { System.out.println("同事类1处理自己的业务逻辑" ); } public void depMethod1 () { this .mediator.execute(); } }
具体同事角色2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package org.example.mediator;public class ConcreteColleague2 extends Colleague { public ConcreteColleague2 (Mediator mediator) { super (mediator); } public void selfMethod1 () { System.out.println("同事类2处理自己的业务逻辑" ); } public void depMethod1 () { super .mediator.execute(); } }
场景实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class App { public static void main (String[] args) { Mediator mediator = new ConcreteMediator(); ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator); ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator); mediator.setConcreteColleague1(colleague1); mediator.setConcreteColleague2(colleague2); colleague1.depMethod1(); colleague2.depMethod1(); } }
应用场景
中介者模式适用于多个对象紧密耦合(蜘蛛网状关系)的情况,让其变为星型。
中介者模式也叫调停者模式,用来调节多个对象之间的混乱关系。
机场调度中心,MVC框架,媒体网关,中介服务都需要使用中介者模式。
中介者模式的优缺点
优点
减少依赖,把原有的一对多的依赖变为一对一的依赖(一个同事对一个中介者)。
缺点
对应的同事太多,中介者类会膨胀。同事类越多,中介者的逻辑就越复杂。