Fork me on GitHub

设计模式之中介者模式

设计模式之中介者模式

1. 什么是中介者模式

Mediator模式也叫中介者模式,是由GoF提出的23种软件设计模式的一种。Mediator模式是行为模式之一,在Mediator模式中,类之间的交互行为被统一放在Mediator的对象中,对象通过Mediator对象同其他对象交互,Mediator对象起着控制器的作用。

中介者模式就是用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

中介者模式的结构:

1

  • mediator中介者类的抽象父类。
  • concreteMediator具体的中介者类。
  • colleague关联类的抽象父类。
  • concreteColleague具体的关联类。

优缺点

优点:

  • 通过将对象彼此解耦,可以增加对象的复用性
  • 通过将控制逻辑集中,可以简化系统维护
  • 可以让对象之间所传递的消息变得简单而且大幅减少
  • 提高系统的灵活性,使得系统易于扩展和维护

缺点:

  • 中介者承担了较多的责任,一旦中介者出现了问题,整个系统就会受到影响
  • 如果设计不当,中介者对象本身变得过于复杂

适用场合:

  • 一组对象之间的通信方式比较复杂,导致相互依赖,结构混乱
  • 一个对象引用很多其他对象并直接与这些对象通信,导致难以复用该对象

2. 具体实例

智慧房屋公司的产品:闹钟、咖啡机、电视机、窗帘等,相互之间需要通信,比如闹钟到什么时候需要通知电视机干什么,通知窗帘干什么,咖啡机要通知窗帘干什么。

这样对象之间需要相互的通信,会形成复杂的网络结构,并且以后想要添加新的设备对象时需要与他进行交互的对象全部需要修改,也就是说这些通信的对象之间的耦合度很高。不利于维护和扩展。

所以这里引入中介者模式来设计这个项目,类图如下:

1539605768

对象之间的通信通过中介者来进行处理,而且每个对象只需要知道中介者就可以了,不需要了解他要通知的对象,这件事请交给中介者去干。这样设计的好处就是对象之间的耦合度降低了,并且添加新的对象的时候只需要与中介者进行交互,不需要改动每一个对象。

具体的代码实现:

中介者接口:

1
2
3
4
5
6
7
public interface Mediator {
public abstract void Register(String colleagueName, Colleague colleague);

public abstract void GetMessage(int stateChange, String colleagueName);

public abstract void SendMessage();
}

具体的中介者实现,也就是在中介者中统筹管理设备之间的通信:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class ConcreteMediator implements Mediator {
private HashMap<String, Colleague> colleagueMap;
private HashMap<String, String> interMap;

public ConcreteMediator() {
colleagueMap = new HashMap<String, Colleague>();
interMap = new HashMap<String, String>();
}

@Override
public void Register(String colleagueName, Colleague colleague) {
// TODO Auto-generated method stub
colleagueMap.put(colleagueName, colleague);

// TODO Auto-generated method stub

if (colleague instanceof Alarm) {
interMap.put("Alarm", colleagueName);
} else if (colleague instanceof CoffeeMachine) {
interMap.put("CoffeeMachine", colleagueName);
} else if (colleague instanceof TV) {
interMap.put("TV", colleagueName);
} else if (colleague instanceof Curtains) {
interMap.put("Curtains", colleagueName);
}

}

@Override
public void GetMessage(int stateChange, String colleagueName) {
// TODO Auto-generated method stub

if (colleagueMap.get(colleagueName) instanceof Alarm) {
if (stateChange == 0) {
((CoffeeMachine) (colleagueMap.get(interMap
.get("CoffeeMachine")))).StartCoffee();
((TV) (colleagueMap.get(interMap.get("TV")))).StartTv();
} else if (stateChange == 1) {
((TV) (colleagueMap.get(interMap.get("TV")))).StopTv();
}

} else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {
((Curtains) (colleagueMap.get(interMap.get("Curtains"))))
.UpCurtains();

} else if (colleagueMap.get(colleagueName) instanceof TV) {

} else if (colleagueMap.get(colleagueName) instanceof Curtains) {

}

}

@Override
public void SendMessage() {
// TODO Auto-generated method stub

}
}

具体的设备的抽象类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public abstract class Colleague {
private Mediator mediator;
public String name;

public Colleague(Mediator mediator, String name) {

this.mediator = mediator;
this.name = name;

}

public Mediator GetMediator() {
return this.mediator;
}

public abstract void SendMessage(int stateChange);
}

设备实现类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Alarm extends Colleague {

public Alarm(Mediator mediator, String name) {
super(mediator, name);
// TODO Auto-generated constructor stub
mediator.Register(name, this);
}

public void SendAlarm(int stateChange) {
SendMessage(stateChange);
}

@Override
public void SendMessage(int stateChange) {
// TODO Auto-generated method stub
this.GetMediator().GetMessage(stateChange, this.name);
}
}
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
public class CoffeeMachine extends Colleague {

public CoffeeMachine(Mediator mediator, String name) {
super(mediator, name);
// TODO Auto-generated constructor stub
mediator.Register(name, this);
}

@Override
public void SendMessage(int stateChange) {
// TODO Auto-generated method stub
this.GetMediator().GetMessage(stateChange, this.name);
}

public void StartCoffee() {
System.out.println("It's time to startcoffee!");
}

public void FinishCoffee() {

System.out.println("After 5 minutes!");
System.out.println("Coffee is ok!");
SendMessage(0);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class TV extends Colleague {

public TV(Mediator mediator, String name) {
super(mediator, name);
// TODO Auto-generated constructor stub
mediator.Register(name, this);
}

@Override
public void SendMessage(int stateChange) {
// TODO Auto-generated method stub
this.GetMediator().GetMessage(stateChange, this.name);
}

public void StartTv() {
// TODO Auto-generated method stub
System.out.println("It's time to StartTv!");
}

public void StopTv() {
// TODO Auto-generated method stub
System.out.println("StopTv!");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Curtains extends Colleague {

public Curtains(Mediator mediator, String name) {
super(mediator, name);
// TODO Auto-generated constructor stub
mediator.Register(name, this);
}

@Override
public void SendMessage(int stateChange) {
// TODO Auto-generated method stub
this.GetMediator().GetMessage(stateChange, this.name);
}

public void UpCurtains() {
System.out.println("I am holding Up Curtains!");
}
}

测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class MainTest {

public static void main(String[] args) {
Mediator mediator = new ConcreteMediator();
Alarm mAlarm = new Alarm(mediator, "mAlarm");
CoffeeMachine mCoffeeMachine = new CoffeeMachine(mediator,
"mCoffeeMachine");
Curtains mCurtains = new Curtains(mediator, "mCurtains");
TV mTV = new TV(mediator, "mTV");
mAlarm.SendAlarm(0);
mCoffeeMachine.FinishCoffee();
mAlarm.SendAlarm(1);
}

}

本文标题:设计模式之中介者模式

文章作者:WilsonSong

发布时间:2018年10月18日 - 14:10

最后更新:2018年10月18日 - 14:10

原始链接:https://songwell1024.github.io/2018/10/18/MediatorPattern/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------