本文共 5187 字,大约阅读时间需要 17 分钟。
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from refering to each other explicitly, and it lets you vary their interaction independently.
中介者模式的结构中包括四种角色。
+ 中介者(Mediator):中介者使一个接口,该接口定义了用于同事(Colleague)对象之间进行通信的方法。 + 具体中介者(Concrete Mediator):具体中介者是实现中介者接口的类。具体中介者需要包含所有具体同事(Concrete Colleague)的引用,并通过实现中介者接口中的方法来满足具体同事之间的通信请求。 + 同事(Colleague):一个接口,规定了具体同事需要实现的方法。 + 具体同事(Concrete Colleague):实现同事接口的类。具体同事需要包含具体中介者的引用,一个具体同事需要和其他同事交互时,只需将自己的请求通知给它所包含的具体中介者即可。package Mediator;public interface Colleague { public void giveMess(String[] mess); public void receiverMess(String mess); public void setName(String name); public String getName();}
package Mediator;public class ConcreteMediator { ColleagueA colleagueA; ColleagueB colleagueB; ColleagueC colleagueC; public void registerColleagueA(ColleagueA colleagueA) { this.colleagueA = colleagueA; } public void registerColleagueB(ColleagueB colleagueB) { this.colleagueB = colleagueB; } public void registerColleagueC(ColleagueC colleagueC) { this.colleagueC = colleagueC; } public void deliverMess(Colleague colleague, String[] mess) { if (colleague == colleagueA) { if (mess.length >= 2) { colleagueB.receiverMess(colleague.getName() + mess[0]); colleagueC.receiverMess(colleague.getName() + mess[1]); } } if (colleague == colleagueB) { if (mess.length >= 2) { colleagueA.receiverMess(colleague.getName() + mess[0]); colleagueC.receiverMess(colleague.getName() + mess[1]); } } if (colleague == colleagueC) { if (mess.length >= 2) { colleagueA.receiverMess(colleague.getName() + mess[0]); colleagueB.receiverMess(colleague.getName() + mess[1]); } } }}
package Mediator;public class ColleagueA implements Colleague { ConcreteMediator cm; String name; public ColleagueA(ConcreteMediator cm) { this.cm = cm; cm.registerColleagueA(this); } @Override public void giveMess(String[] mess) { cm.deliverMess(this, mess); } @Override public void receiverMess(String mess) { System.out.println(name + "收到的信息"); System.out.println("\t" + mess); } @Override public void setName(String name) { this.name = name; } @Override public String getName() { return this.name; }}
package Mediator;public class ColleagueB implements Colleague { ConcreteMediator cm; String name; public ColleagueB(ConcreteMediator cm) { this.cm = cm; cm.registerColleagueB(this); } @Override public void giveMess(String[] mess) { cm.deliverMess(this, mess); } @Override public void receiverMess(String mess) { System.out.println(name + "收到的信息"); System.out.println("\t" + mess); } @Override public void setName(String name) { this.name = name; } @Override public String getName() { return this.name; }}
package Mediator;public class ColleagueC implements Colleague { ConcreteMediator cm; String name; public ColleagueC(ConcreteMediator cm) { this.cm = cm; cm.registerColleagueC(this); } @Override public void giveMess(String[] mess) { cm.deliverMess(this, mess); } @Override public void receiverMess(String mess) { System.out.println(name + "收到的信息"); System.out.println("\t" + mess); } @Override public void setName(String name) { this.name = name; } @Override public String getName() { return this.name; }}
package Mediator;public class Appication { public static void main(String[] args) { ConcreteMediator cm = new ConcreteMediator(); ColleagueA colleagueA = new ColleagueA(cm); ColleagueB colleagueB = new ColleagueB(cm); ColleagueC colleagueC = new ColleagueC(cm); colleagueA.setName("A国"); colleagueB.setName("B国"); colleagueC.setName("C国"); String[] messA = { "要求归还曾抢夺的100斤土豆", "要求归还曾抢夺的20头牛"}; colleagueA.giveMess(messA); String[] messB = { "要求归还曾抢夺的10只公鸡", "要求归还曾抢夺的15匹马"}; colleagueB.giveMess(messB); String[] messC = { "要求归还曾抢夺的300斤小麦", "要求归还曾抢夺的50头驴"}; colleagueC.giveMess(messC); }}
转载地址:http://uiszo.baihongyu.com/