中介者模式的缺点是显而易见的,因为这个“中介“承担了较多的责任,所以一旦这个中介对象出现了问题,那么整个系统就会受到重大的影响。
代码分3个部分,第一部分显示2个对象相互交互,第二个代码部分显示使用中介者模式的好处。第三个也是中介者模式,只是数量多一点,使用List保存对象Colleague。
第一部分显示2个对象相互交互
package com.lee.desingerPattener23.mediator.dirty; abstract class AbstractColleague { protected int number; public int getNumber() { return number; } public void setNumber(int number){ this.number = number; } //抽象方法,修改数字时同时修改关联对象 public abstract void setNumber(int number, AbstractColleague coll); } class ColleagueA extends AbstractColleague{ public void setNumber(int number, AbstractColleague coll) { this.number = number; coll.setNumber(number*100); } } class ColleagueB extends AbstractColleague{ public void setNumber(int number, AbstractColleague coll) { this.number = number; coll.setNumber(number/100); } } public class DirtyClient { public static void main(String[] args){ AbstractColleague collA = new ColleagueA(); AbstractColleague collB = new ColleagueB(); System.out.println("==========设置A影响B=========="); collA.setNumber(1288, collB); System.out.println("collA的number值:"+collA.getNumber()); System.out.println("collB的number值:"+collB.getNumber()); System.out.println("==========设置B影响A=========="); collB.setNumber(87635, collA); System.out.println("collB的number值:"+collB.getNumber()); System.out.println("collA的number值:"+collA.getNumber()); } }
第二个代码部分显示使用中介者模式的好处。2个对象不直接关联
package com.lee.desingerPattener23.mediator.example1; // http://lastsoul.iteye.com/blog/1883881 abstract class AbstractColleague { protected int number; public int getNumber() { return number; } public void setNumber(int number){ this.number = number; } //注意这里的参数不再是同事类,而是一个中介者 public abstract void setNumber(int number, AbstractMediator am); } class ColleagueA extends AbstractColleague{ public void setNumber(int number, AbstractMediator am) { this.number = number; am.AaffectB(); } } class ColleagueB extends AbstractColleague{ @Override public void setNumber(int number, AbstractMediator am) { this.number = number; am.BaffectA(); } } abstract class AbstractMediator { protected AbstractColleague A; protected AbstractColleague B; public AbstractMediator(AbstractColleague a, AbstractColleague b) { A = a; B = b; } public abstract void AaffectB(); public abstract void BaffectA(); } class Mediator extends AbstractMediator { public Mediator(AbstractColleague a, AbstractColleague b) { super(a, b); } //处理A对B的影响 public void AaffectB() { int number = A.getNumber(); B.setNumber(number*100); } //处理B对A的影响 public void BaffectA() { int number = B.getNumber(); A.setNumber(number/100); } } public class MediatorClient { public static void main(String[] args){ AbstractColleague collA = new ColleagueA(); AbstractColleague collB = new ColleagueB(); AbstractMediator am = new Mediator(collA, collB); System.out.println("==========通过设置A影响B=========="); collA.setNumber(1000, am); System.out.println("collA的number值为:"+collA.getNumber()); System.out.println("collB的number值为A的10倍:"+collB.getNumber()); System.out.println("==========通过设置B影响A=========="); collB.setNumber(1000, am); System.out.println("collB的number值为:"+collB.getNumber()); System.out.println("collA的number值为B的0.1倍:"+collA.getNumber()); } }
第三个也是中介者模式,稍复杂一些,只是数量多一点,使用List保存对象Colleague。
package com.lee.desingerPattener23.mediator.example2; import java.util.ArrayList; import java.util.List; abstract class Colleague { // 引入中介者 private Mediator mediator; // 消息 private String message; public Colleague(Mediator m) { mediator = m; } public Mediator getMediator() { return mediator; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } // 发送消息 public abstract void sendMsg(); // 收到消息 public abstract void getMsg(String msg); // 发送消息 //注意!这个是有参的sendMsg public void sendMsg(String msg) { this.message = msg; mediator.action(this); } } class Colleague1 extends Colleague { public Colleague1(Mediator m) { super(m); } public void getMsg(String msg) { System.out.println("Colleague1 has get the message -'" + msg + "'"); } //注意!这个是无参的sendMsg public void sendMsg() { System.out.println("Colleague1 has send the message '" + getMessage()+ "'"); } } class Colleague2 extends Colleague { public Colleague2(Mediator m) { super(m); } public void getMsg(String msg) { System.out.println("Colleague2 has get the message -'" + msg + "'"); } public void sendMsg() { System.out.println("Colleague2 has send the message '" + getMessage()+ "'"); } } class Colleague3 extends Colleague { public Colleague3(Mediator m) { super(m); } public void getMsg(String msg) { System.out.println("Colleague3 has get the message -'" + msg + "'"); } public void sendMsg() { System.out.println("Colleague3 has send the message '" + getMessage()+ "'"); } } // 中介者 abstract class Mediator { // Mediator针对Colleague的一个交互行为 public abstract void action(Colleague sender); // 加入Colleague对象 public abstract void addCollegue(Colleague colleague); } class ConcreteMediator extends Mediator { private List<Colleague> colleagues = new ArrayList<Colleague>(0); public void addCollegue(Colleague colleague) { colleagues.add(colleague); } public void action(Colleague actor) { String msg = actor.getMessage(); // send msg for (Colleague colleague : colleagues) { if (colleague.equals(actor)) { colleague.sendMsg(); break; } } // get msg for (Colleague colleague : colleagues) { if (colleague.equals(actor)) continue; else colleague.getMsg(msg); } } } // 测试类 public class MediatorTest { public static void main(String[] args) { // 生成中介者 并注入到各个Colleague对象中 Mediator mediator = new ConcreteMediator(); Colleague colleague1 = new Colleague1(mediator); Colleague colleague2 = new Colleague2(mediator); Colleague colleague3 = new Colleague3(mediator); // 注册对象到中介 mediator.addCollegue(colleague1); mediator.addCollegue(colleague2); mediator.addCollegue(colleague3); // Colleague1 触发行为 colleague1.sendMsg("嗨,大家好!"); System.out.println(); // Colleague2 触发行为 colleague2.sendMsg("很高兴见到你!"); System.out.println(); // Colleague3 触发行为 colleague3.sendMsg("我们一起玩游戏吧!"); System.out.println(); } }