178 lines
4.7 KiB
Java
178 lines
4.7 KiB
Java
|
## 5. 中介者(Mediator)
|
|||
|
|
|||
|
### Intent
|
|||
|
|
|||
|
集中相关对象之间复杂的沟通和控制方式。
|
|||
|
|
|||
|
### Class Diagram
|
|||
|
|
|||
|
|
|||
|
|
|||
|
- Mediator:中介者,定义一个接口用于与各同事(Colleague)对象通信。
|
|||
|
- Colleague:同事,相关对象
|
|||
|
|
|||
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/30d6e95c-2e3c-4d32-bf4f-68128a70bc05.png"/> </div><br>
|
|||
|
|
|||
|
### Implementation
|
|||
|
|
|||
|
Alarm(闹钟)、CoffeePot(咖啡壶)、Calendar(日历)、Sprinkler(喷头)是一组相关的对象,在某个对象的事件产生时需要去操作其它对象,形成了下面这种依赖结构:
|
|||
|
|
|||
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/82cfda3b-b53b-4c89-9fdb-26dd2db0cd02.jpg"/> </div><br>
|
|||
|
|
|||
|
使用中介者模式可以将复杂的依赖结构变成星形结构:
|
|||
|
|
|||
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/5359cbf5-5a79-4874-9b17-f23c53c2cb80.jpg"/> </div><br>
|
|||
|
|
|||
|
```java
|
|||
|
public abstract class Colleague {
|
|||
|
public abstract void onEvent(Mediator mediator);
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
public class Alarm extends Colleague {
|
|||
|
|
|||
|
@Override
|
|||
|
public void onEvent(Mediator mediator) {
|
|||
|
mediator.doEvent("alarm");
|
|||
|
}
|
|||
|
|
|||
|
public void doAlarm() {
|
|||
|
System.out.println("doAlarm()");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
public class CoffeePot extends Colleague {
|
|||
|
@Override
|
|||
|
public void onEvent(Mediator mediator) {
|
|||
|
mediator.doEvent("coffeePot");
|
|||
|
}
|
|||
|
|
|||
|
public void doCoffeePot() {
|
|||
|
System.out.println("doCoffeePot()");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
public class Calender extends Colleague {
|
|||
|
@Override
|
|||
|
public void onEvent(Mediator mediator) {
|
|||
|
mediator.doEvent("calender");
|
|||
|
}
|
|||
|
|
|||
|
public void doCalender() {
|
|||
|
System.out.println("doCalender()");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
public class Sprinkler extends Colleague {
|
|||
|
@Override
|
|||
|
public void onEvent(Mediator mediator) {
|
|||
|
mediator.doEvent("sprinkler");
|
|||
|
}
|
|||
|
|
|||
|
public void doSprinkler() {
|
|||
|
System.out.println("doSprinkler()");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
public abstract class Mediator {
|
|||
|
public abstract void doEvent(String eventType);
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
public class ConcreteMediator extends Mediator {
|
|||
|
private Alarm alarm;
|
|||
|
private CoffeePot coffeePot;
|
|||
|
private Calender calender;
|
|||
|
private Sprinkler sprinkler;
|
|||
|
|
|||
|
public ConcreteMediator(Alarm alarm, CoffeePot coffeePot, Calender calender, Sprinkler sprinkler) {
|
|||
|
this.alarm = alarm;
|
|||
|
this.coffeePot = coffeePot;
|
|||
|
this.calender = calender;
|
|||
|
this.sprinkler = sprinkler;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void doEvent(String eventType) {
|
|||
|
switch (eventType) {
|
|||
|
case "alarm":
|
|||
|
doAlarmEvent();
|
|||
|
break;
|
|||
|
case "coffeePot":
|
|||
|
doCoffeePotEvent();
|
|||
|
break;
|
|||
|
case "calender":
|
|||
|
doCalenderEvent();
|
|||
|
break;
|
|||
|
default:
|
|||
|
doSprinklerEvent();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void doAlarmEvent() {
|
|||
|
alarm.doAlarm();
|
|||
|
coffeePot.doCoffeePot();
|
|||
|
calender.doCalender();
|
|||
|
sprinkler.doSprinkler();
|
|||
|
}
|
|||
|
|
|||
|
public void doCoffeePotEvent() {
|
|||
|
// ...
|
|||
|
}
|
|||
|
|
|||
|
public void doCalenderEvent() {
|
|||
|
// ...
|
|||
|
}
|
|||
|
|
|||
|
public void doSprinklerEvent() {
|
|||
|
// ...
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
public class Client {
|
|||
|
public static void main(String[] args) {
|
|||
|
Alarm alarm = new Alarm();
|
|||
|
CoffeePot coffeePot = new CoffeePot();
|
|||
|
Calender calender = new Calender();
|
|||
|
Sprinkler sprinkler = new Sprinkler();
|
|||
|
Mediator mediator = new ConcreteMediator(alarm, coffeePot, calender, sprinkler);
|
|||
|
// 闹钟事件到达,调用中介者就可以操作相关对象
|
|||
|
alarm.onEvent(mediator);
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
doAlarm()
|
|||
|
doCoffeePot()
|
|||
|
doCalender()
|
|||
|
doSprinkler()
|
|||
|
```
|
|||
|
|
|||
|
### JDK
|
|||
|
|
|||
|
- All scheduleXXX() methods of [java.util.Timer](http://docs.oracle.com/javase/8/docs/api/java/util/Timer.html)
|
|||
|
- [java.util.concurrent.Executor#execute()](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html#execute-java.lang.Runnable-)
|
|||
|
- submit() and invokeXXX() methods of [java.util.concurrent.ExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html)
|
|||
|
- scheduleXXX() methods of [java.util.concurrent.ScheduledExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledExecutorService.html)
|
|||
|
- [java.lang.reflect.Method#invoke()](http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#invoke-java.lang.Object-java.lang.Object...-)
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div align="center"><img width="320px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/githubio/公众号二维码-2.png"></img></div>
|