中介者模式定义:用一个中介对象来封装一系列的对象交互。中介者使得各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
中介者模式本质:封装交互
参考:http://chjavach.iteye.com/blog/1626265,研磨设计模式(书籍),大话设计模式(书籍)
模式图:
待补充
(1)交互对象的父类(Abstractor.java)
package com.mcc.core.designPattern.behavior.mediator.simpleMediator; /** * 交互对象的父类 * * @author <a href="mailto:[email protected]">menergy</a> * DateTime: 14-3-15 下午1:38 */ public abstract class Abstractor { //持有中介者对象 private Mediator mediator; /** * 构造器,传入中介者对象 * @param mediator */ public Abstractor(Mediator mediator){ this.mediator = mediator; } /** * 获取中介者对象 * @return */ public Mediator getMediator(){ return mediator; } } |
(2)交互类实现A(AbstractorExtendsA.java)
package com.mcc.core.designPattern.behavior.mediator.simpleMediator; /** * 交互类实现A * * @author <a href="mailto:[email protected]">menergy</a> * DateTime: 14-3-15 下午1:43 */ public class AbstractorExtendsA extends Abstractor { /** * 构造器,传入中介者对象 * * @param mediator */ public AbstractorExtendsA(Mediator mediator) { super(mediator); } public void someMethodA(){ System.out.println("实现类A的方法操作"); //交给中介者处理接下来的工作,打算交给B处理 this.getMediator().handle(this); } } |
(3)交互类实现B(AbstractorExtendsB.java)
package com.mcc.core.designPattern.behavior.mediator.simpleMediator; /** * 交互类实现B * * @author <a href="mailto:[email protected]">menergy</a> * DateTime: 14-3-15 下午1:49 */ public class AbstractorExtendsB extends Abstractor { /** * 构造器,传入中介者对象 * * @param mediator */ public AbstractorExtendsB(Mediator mediator) { super(mediator); } public void someMethodB(){ System.out.println("实现类B的方法操作"); //交给中介者处理接下来的工作,打算交给C工作 this.getMediator().handle(this); } } |
(4)交互类实现C(AbstractorExtendsC.java)
package com.mcc.core.designPattern.behavior.mediator.simpleMediator; /** * 交互类实现C * * @author <a href="mailto:[email protected]">menergy</a> * DateTime: 14-3-15 下午1:51 */ public class AbstractorExtendsC extends Abstractor { /** * 构造器,传入中介者对象 * * @param mediator */ public AbstractorExtendsC(Mediator mediator) { super(mediator); } public void someMethodC(){ System.out.println("实现类C的方法操作"); //交给中介者处理接下来的工作,打算交给C工作 System.out.println("交互工作完成,移交处理"); } } |
(5)中介者接口(Mediator.java)
package com.mcc.core.designPattern.behavior.mediator.simpleMediator; /** * 中介者接口 * * @author <a href="mailto:[email protected]">menergy</a> * DateTime: 14-3-15 下午1:34 */ public interface Mediator { /** * 处理交互的方法 * @param abstractor 交互类父类 */ public void handle(Abstractor abstractor); } |
(6)中介者(MediatorImpl.java)
package com.mcc.core.designPattern.behavior.mediator.simpleMediator; /** * 中介者,主要实现交互类间的交互工作 * * 中介者模式的定义:用一个中介对象来封装一系列的对象交互。中介者使得各对象不需要显式地相互引用, * 从而使其耦合松散,而且可以独立地改变它们之间的交互。 * 中介者模式的本质:封装交互 * * @author <a href="mailto:[email protected]">menergy</a> * DateTime: 14-3-15 下午1:55 */ public class MediatorImpl implements Mediator { //中介者知道交互类 private AbstractorExtendsA abstractorExtendsA; private AbstractorExtendsB abstractorExtendsB; private AbstractorExtendsC abstractorExtendsC; public void setAbstractorExtendsA(AbstractorExtendsA abstractorExtendsA) { this.abstractorExtendsA = abstractorExtendsA; } public void setAbstractorExtendsB(AbstractorExtendsB abstractorExtendsB) { this.abstractorExtendsB = abstractorExtendsB; } public void setAbstractorExtendsC(AbstractorExtendsC abstractorExtendsC) { this.abstractorExtendsC = abstractorExtendsC; } /** * 处理交互工作 * @param abstractor 交互类父类 */ @Override public void handle(Abstractor abstractor) { //处理交互,这里可以任意组合交互,可结合其它设计模式,可加入其它操作 if(abstractor == abstractorExtendsA){ abstractorExtendsB.someMethodB(); }else if(abstractor == abstractorExtendsB){ abstractorExtendsC.someMethodC(); } } } |
(7)客户端测试(Client.java)
package com.mcc.core.designPattern.behavior.mediator.simpleMediator; /** * 客户端测试 * * @author <a href="mailto:[email protected]">menergy</a> * DateTime: 14-3-15 下午2:09 */ public class Client { public static void main(String args[]){ //创建中介者 MediatorImpl mediatorImpl = new MediatorImpl(); //创建交互类 AbstractorExtendsA abstractorA = new AbstractorExtendsA(mediatorImpl); AbstractorExtendsB abstractorB = new AbstractorExtendsB(mediatorImpl); AbstractorExtendsC abstractorC = new AbstractorExtendsC(mediatorImpl); //让中介者知道交互类 mediatorImpl.setAbstractorExtendsA(abstractorA); mediatorImpl.setAbstractorExtendsB(abstractorB); mediatorImpl.setAbstractorExtendsC(abstractorC); //测试交互 System.out.println("\n开始处理,从交互类A发起:\n"); abstractorA.someMethodA(); //测试交互 System.out.println("\n开始处理,从交互类B发起:\n"); abstractorB.someMethodB(); //测试交互 System.out.println("\n开始处理,从交互类C发起:\n"); abstractorC.someMethodC(); } } |
(1)交互类实现A(AbstractorExtendsA.java)
package com.mcc.core.designPattern.behavior.mediator.extendMediator; /** * 交互类实现A * * @author <a href="mailto:[email protected]">menergy</a> * DateTime: 14-3-15 下午1:43 */ public class AbstractorExtendsA{ public void handle(){ someMethodA(); //获取单一实例 MediatorImpl mediatorImpl = MediatorImpl.getMediator(); //交给中介者处理接下来的工作,打算交给B处理 mediatorImpl.handleAtoB(); } public void someMethodA(){ System.out.println("实现类A的方法操作"); } } |
(2)交互类实现B(AbstractorExtendsB.java)
package com.mcc.core.designPattern.behavior.mediator.extendMediator; /** * 交互类实现B * * @author <a href="mailto:[email protected]">menergy</a> * DateTime: 14-3-15 下午1:49 */ public class AbstractorExtendsB { public void handle(){ someMethodB(); //获取单一实例 MediatorImpl mediatorImpl = MediatorImpl.getMediator(); //交给中介者处理接下来的工作,打算交给A工作 mediatorImpl.handleBtoA(); } public void someMethodB(){ System.out.println("实现类B的方法操作"); } } |
(3)中介者类(MediatorImpl.java)
package com.mcc.core.designPattern.behavior.mediator.extendMediator; /** * 中介者类,广义中介者模式,主要实现交互类间的交互工作 * * @author <a href="mailto:[email protected]">menergy</a> * DateTime: 14-3-15 下午2:27 */ public class MediatorImpl{ ////单例模式 private static MediatorImpl mediator = new MediatorImpl(); //私有构造器 private MediatorImpl(){ } //获得唯一实例 public static MediatorImpl getMediator(){ return mediator; } //中介者知道交互类 private AbstractorExtendsA abstractorExtendsA; private AbstractorExtendsB abstractorExtendsB; public void setAbstractorExtendsA(AbstractorExtendsA abstractorExtendsA) { this.abstractorExtendsA = abstractorExtendsA; } public void setAbstractorExtendsB(AbstractorExtendsB abstractorExtendsB) { this.abstractorExtendsB = abstractorExtendsB; } /** * 处理A到B交互 */ public void handleAtoB() { abstractorExtendsB.someMethodB(); } /** * 处理B到A交互 */ public void handleBtoA() { abstractorExtendsA.someMethodA(); } } |
(4)客户端测试(Client.java)
package com.mcc.core.designPattern.behavior.mediator.extendMediator; /** * 客户端测试 * * @author <a href="mailto:[email protected]">menergy</a> * DateTime: 14-3-15 下午2:41 */ public class Client { public static void main(String args[]){ //创建中介者 MediatorImpl mediatorImpl = MediatorImpl.getMediator(); //创建交互类 AbstractorExtendsA abstractorA = new AbstractorExtendsA(); AbstractorExtendsB abstractorB = new AbstractorExtendsB(); //让中介者知道交互类 mediatorImpl.setAbstractorExtendsA(abstractorA); mediatorImpl.setAbstractorExtendsB(abstractorB); //测试交互 System.out.println("\n开始处理,从交互类A发起,交给B:\n"); abstractorA.handle(); //测试交互 System.out.println("\n开始处理,从交互类B发起,交给A:\n"); abstractorB.handle(); } } |