Java深入到一定程度,就不可避免的碰到设计模式这一概念,了解设计模式,将使自己对java中的接口或抽象类应用有更深的理解.设计模式在java的中型系统中应用广泛,遵循一定的编程模式,才能使自己的代码便于理解,易于交流,Mediator(中介者模式)模式是比较常用的一个模式.
Mediator中介者模式,当多个对象彼此间都有联系的时候,我们就可以应用Mediator将对象间的多对多关系转换为一对多的关系,这样做,可以使各个对象间的耦合松散。统一管理对象间的交互。但也可能使得Mediator对象成为一个系统中的庞然大物,难以维护
使用场景:集中负责维护对象模型的关系完整性 以及需要封装对象间交互方式的时候.
其实MVC中的controller就是一种Mediator,是UI层和后端应用sevice层间的中介者。中介者将交互的复杂性变为中介者的复杂性
在此写了7个java类来描述说明Mediator设计模式的实现方式;
1、Colleague.java 交互对象的抽象类
2、Colleague1.java 交互对象1
3、Colleague2.java 交互对象2
4、Colleague3.java 交互对象3
5、Mediator.java 中介者抽象类
6、ConcreteMediator.java 具体的中介者
7、MediatorTest.java 带有main方法的测试类
=============== 1、Colleague.java
交互对象的抽象类,定义了中介者的注入方法 、交互的行为方法
package mediator;
public abstract class Colleague {
//中介者
private Mediator mediator;
public Mediator getMediator() {
returnmediator;
}
public Colleague(Mediator m) {
mediator =m;
}
//消息
private String message;
public String getMessage() {
returnmessage;
}
public void setMessage(String message) {
this.message= message;
}
//发送消息
public abstract void sendMsg();
//收到消息
public abstract void getMsg(String msg);
//发送消息
public void sendMsg(String msg) {
this.message= msg;
mediator.action(this);
}
}
=============== 1 end
=============== 2、Colleague1.java
package mediator;
public class Colleague1 extends Colleague {
public Colleague1(Mediator m) {
super(m);
}
public void getMsg(String msg) {
System.out.println("Colleague1 has got themessage -'" + msg + "'");
}
public void sendMsg() {
System.out.println("Colleague1 has send the message '" +getMessage() + "'");
}
}
=============== 2 end
=============== 3、Colleague2.java
package mediator;
public class Colleague2 extends Colleague {
public Colleague2(Mediator m) {
super(m);
}
public void getMsg(String msg) {
System.out.println("Colleague2 has got themessage -'" + msg + "'");
}
public void sendMsg() {
System.out.println("Colleague2 has send the message '" +getMessage() + "'");
}
}
=============== 3 end
=============== 4、Colleague3.java
package mediator;
public class Colleague3 extends Colleague {
public Colleague3(Mediator m) {
super(m);
}
public void getMsg(String msg) {
System.out.println("Colleague3 has got themessage -'" + msg + "'");
}
public void sendMsg() {
System.out.println("Colleague3 has send the message '" +getMessage() + "'");
}
}
=============== 4 end
=============== 5、Mediator.java
package mediator;
abstract class Mediator {
//Mediator针对Colleague的一个交互行为
public abstract void action(Colleaguesender);
//加入Colleague对象
public abstract void addCollegue(Colleaguecolleague);
}
=============== 5 end
=============== 6、ConcreteMediator.java
具体的中介者,负责管理Colleague对象间的关系、以及Colleague对象间的交互
package mediator;
import java.util.ArrayList;
import java.util.List;
public class ConcreteMediator extends Mediator {
privateList<Colleague> colleagues = newArrayList<Colleague>(0);
public void addCollegue(Colleague colleague){
colleagues.add(colleague);
}
public void action(Colleague actor) {
String msg =actor.getMessage();
//sendmsg
for(Colleague colleague : colleagues) {
if(colleague.equals(actor)){
colleague.sendMsg();
break;
}
}
//gotmsg
for(Colleague colleague : colleagues) {
if(colleague.equals(actor))
continue;
colleague.getMsg(msg);
}
}
}
=============== 6 end
=============== 7、MediatorTest.java
package mediator;
public class MediatorTest {
public static void main(String[] args){
//生成中介者并注入到各个Colleague对象中
Mediatormediator = new ConcreteMediator();
Colleaguecolleague1 = new Colleague1(mediator);
Colleaguecolleague2 = new Colleague2(mediator);
Colleaguecolleague3 = new Colleague3(mediator);
//注册对象到中介
mediator.addCollegue(colleague1);
mediator.addCollegue(colleague2);
mediator.addCollegue(colleague3);
//Colleague1 触发行为
colleague1.sendMsg("Hi,it's time to lunch. Let's go!");
System.out.println();
//Colleague2触发行为
colleague2.sendMsg("Is anybody here!");
System.out.println();
//Colleague3触发行为
colleague3.sendMsg("Wait!I will lunch off right away.");
System.out.println();
}
}
=============== 7 end