模式定义
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要相互显示引用,从而使耦合松散,而且可以独立的改变它们之间的交互。
模式结构
1角色
中介者模式包含4个角色:
中介者(Mediator):一个接口,该接口定义 了用于同事对象之间进行通信的方法。
具体中介者:是实现中介者接口的类。
同事(colleague):一个接口,规定了需要实现的方法。
具体同事:实现同事接口的类。具体同事需要包含具体中介者的引用,一个具体同事需要和其他具体同事交互时,只需将自己请求通知给它所包含的具体中介者即可。
注:如果仅需要一个具体的中介者,模式中的中介者接口可省略。
2结构描述
同事
Colleague.java
public interface Colleague{
public void giveMess(String[] mess);
public void receiverMess(String mess);
public void setName(String name);
public String getName();
}
具体中介者
ConcreteMediator.java
public class ConcreteMediator{
ColleagueA colleagueA;
ColleagueB colleagueB;
ColleagueC colleagueC;
public void registerColleagueA(ColleagueA colleagueA)
{
this.colleagueA = colleagueA;
}
public void registerColleagueB(ColleagueA 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]);
}
}
else if(colleague==colleagueB){
if(mess.length>=2){
colleagueA.receiverMess(colleague.geName()+mess[0]);
colleagueC.receiverMess(colleague.geName()+mess[1]);
}
}
else if(colleague==colleagueC){
if(mess.length>=2){
colleagueA.reciverMess(colleague.getName()+mess[0]);
colleagueB.reciverMess(colleague.getName()+mess[1]);
}
]
}
}
具体同事
ColleagueA.java
public class ColleagueA implements Colleague{
ConcreteMediator mediator;
String name;
ColleagueA(ConcreteMediator mediator){
this.mediator=mediator;
meidator.registerColleagueA(this);
}
public void giveMess(String[] mess){
mediator.deliverMess(this,mess);
}
public void receiverMess(String mess){
System.out.println(name+"receive mess!");
System.out.println("\t"+mess);
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
具体同事
ColleagueB.java
public class ColleagueB implements Colleague{
ConcreteMediator mediator;
String name;
ColleagueB(ConcreteMediator mediator){
this.mediator=mediator;
meidator.registerColleagueB(this);
}
public void giveMess(String[] mess){
mediator.deliverMess(this,mess);
}
public void receiverMess(String mess){
System.out.println(name+"receive mess!");
System.out.println("\t"+mess);
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
具体同事
ColleagueC.java
public class ColleagueC implements Colleague{
ConcreteMediator mediator;
String name;
ColleagueC(ConcreteMediator mediator){
this.mediator=mediator;
meidator.registerColleagueC(this);
}
public void giveMess(String[] mess){
mediator.deliverMess(this,mess);
}
public void receiverMess(String mess){
System.out.println(name+"receive mess!");
System.out.println("\t"+mess);
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
模式使用
Application.java
public class Application{
public static void main(string args[]){
ConcreteMediator mediator = new ConcreteMediator();
ColleagueA colleagueA = new ColleagueA(mediator);
ColleagueA colleagueB = new ColleagueB(mediator);
ColleagueA colleagueC = new ColleagueC(mediator);
colleagueA.setName("A");
colleagueB.setName("B");
colleagueC.setName("C");
String[] messA ={"mess from A to B","mess from A to C"};
String[] messB ={"mess from B to A","mess from B to C"};
String[] messC ={"mess from C to A","mess from C to B"};
colleagueA.giveMess(messA);
colleagueB.giveMess(messB);
colleagueC.giveMess(msssC);
}
}
模式优点
避免多对象互相引用,否则不仅系统难于维护,且也使其他系统难以复用这些对象。
可以通过中介者模式,使原本分布于多个对象之间的交互行为集中在一起。当这些对象之间需要改变通信行为时,只需要使用一个具体中介者即可,不必修改各个具体同事的代码,即这些同事可以被重用。
中介者使各个具体的同事之间解耦,修改任何一个具体同事不会影响其他同事。
具体中介者集中了同事之间的交互细节,使得系统比较清楚地知道整个系统中的同事是如何交互的。
当一些对象想互相通信,但又无法相互包含对方的引用,使用中介者模式可以使他们之间交互。
使用场景
系统中许多对象以复杂方式交互,所导致的依赖关系使系统难以理解和维护,这时应考虑使用中介者模式。