中介者模式 用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
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):实现同事接口的类。具体同事需要包含具体中介者的引用,一个具体同事需要和其他同事交互时,只需将自己的请求通知给它所包含的具体中介者即可。
简单的例子 同事接口类Colleague.java 1
2
3
4
5
6
7
8
package Mediator;
public interface Colleague {
public void giveMess (String[] mess) ;
public void receiverMess (String mess) ;
public void setName (String name) ;
public String getName () ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 ]);
}
}
}
}
ConcreteColleague的实现类ColleagueA.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
}
}
ConcreteColleague的实现类ColleagueB.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
}
}
ConcreteColleague的实现类ColleagueC.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
}
}
测试类Application.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
}
}
运行截图
中介者模式的优点
可以避免许多的对象为了之间的通信而相互显示引用,否则,不仅系统难于维护,而且也使其他系统难以复用这些对象。
可以通过中介者将原本分布于多个对象之间的交互行为集中在一起。当这些对象之间需要改变之间的通信行为时,只需使用一个具体中介者即可,不必修改各个具体同事的代码,即这些同事可被重用。
具体中介者使得各个具体同事完全解耦,修改任何一个具体同事的代码不会影响到其他同事。
具体中介者集中了同事之间是如何交互的细节,使系统比较清楚地知道整个系统中同事是如何交互的。
当一些对象想相互通信,但又无法相互包含对方的引用,那么使用中介者模式就可以使这些对象互相通信。 注:由于具体中介者集中了同事之间是如何交互的细节,可能使具体具体中介者变得非常复杂,增加了维护的难度。
适用中介者模式的情景
许多对象以复杂的方式交,所导致的依赖关系使系统难以理解和维护。
一个对象引用其他很多对象,导致难以复用该对象。
下载源码请到 MyGitHub