中介者模式是由GoF提出的23种软件设计模式的一种。Mediator模式是行为模式之一,Mediator模式定义:用一个中介者对象来封装一系列的对象交互。中介者使各对象不需要显式的相互引用,从而使其耦合松散,而且可以独立的改变他们之间的交互。
适用性
1)一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。
2)一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。
3)想定制一个分布在多个类的行为,而又不想生成太多的子类。
结构图如下
colleague向一个中介者对象发送和接受请求。中介者在各个colleague间适当的转发请求以实现协作行为。
优缺点
1)减少了子类生成Mediator将原本分布于多个对象间的行为集中在一起,改变这些行为只需生成Mediator的子类即可,这样各个Colleague类可被重用。
2)它将各Colleague解耦。Mediator有利于各Colleague间的松耦合,你可以独立的改变和复用各Colleague类和Mediator类。
3)它简化了对象协议用Mediator和各Colleague间的一对多的交互来代替多对多的交互。一对多的关系更易于理解、维护和扩展。
4)它对对象如何协作进行了抽象将中介作为一个独立的概念并将其封装在一个对象中,使你将注意力从对象各自本身的行为转移到它们之间的交互上来。这有助于弄清楚一个系统中的对象是如何交互的。
5)它使控制集中化,中介者模式将交互的复杂性变为中介者的复杂性。因为中介者封装了协议,它可能变得比任一个Colleague都复杂。这可能使得中介者自身成为一个难于维护的庞然大物。
1、Colleague.java:
package mediator;
public 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(Colleague colleague);
public abstract void getMsg(Colleague colleague);
}
2、Colleague的子类:Colleague1,Colleague2,Colleague3
package mediator;
public class Colleague1 extends Colleague{
public Colleague1(Mediator m){
super(m);
}
@Override
public void getMsg(Colleague colleague) {
getMediator().action(colleague,0);
//System.out.println("Colleague1 has got the message-"+"");
}
@Override
public void sendMsg(Colleague colleague) {
getMediator().action(colleague,1);
//System.out.println("Colleague1 has send the message "+msg+"");
}
}
package mediator;
public class Colleague2 extends Colleague{
public Colleague2(Mediator m){
super(m);
}
@Override
public void getMsg(Colleague colleague) {
getMediator().action(colleague,0);
//System.out.println("Colleague2 has got the message-"+"");
}
@Override
public void sendMsg(Colleague colleague) {
getMediator().action(colleague,1);
//System.out.println("Colleague2 has send the message "+msg+"");
}
}
package mediator;
public class Colleague3 extends Colleague{
public Colleague3(Mediator m){
super(m);
}
@Override
public void getMsg(Colleague colleague) {
getMediator().action(colleague,0);
//System.out.println("Colleague3 has got the message-"+"");
}
@Override
public void sendMsg(Colleague colleague) {
getMediator().action(colleague,1);
//System.out.println("Colleague3 has send the message "+msg+"");
}
}
3、Mediator抽象类:
package mediator;
public abstract class Mediator {
/**
* @param sender
* @param flag 0是读,1是发送
*/
public abstract void action(Colleague sender,int flag);//mediator针对colleague的一个交互行为
public abstract void addCollegue(Colleague colleague);//加入mediator对象
}
4、Mediator的子类:
package mediator;
import java.util.ArrayList;
import java.util.List;
public class ConcreteMediator extends Mediator{
private List<Colleague> member = new ArrayList<Colleague>();
@Override
public void action(Colleague sender,int flag) {
String msg = sender.getMessage();
for(Colleague colleague : member){
if(colleague.equals(sender)){
if(flag==0){
System.out.println("这是从"+sender+"接收到的消息:"+sender.getMessage());
}else{
sender.setMessage("test.......");
}
break;
}
}
}
@Override
public void addCollegue(Colleague colleague) {
member.add(colleague);
}
}
5、测试类:
package mediator;
public class MediatorTEST {
public static void main(String[] args){
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.setMessage("嗨,大家好!");
// Colleague2 触发行为
colleague2.setMessage("很高兴见到你!");
// Colleague3 触发行为
colleague3.setMessage("我们一起玩游戏吧!");
colleague1.getMsg(colleague2);
colleague1.sendMsg(colleague2);
colleague1.getMsg(colleague2);
}
}