mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-30 17:38:43 +00:00
Update 设计模式 - 中介者.md
小节标题改为中文,重新绘制类图,补充参考资料。
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 226 KiB |
+11
-12
@@ -1,20 +1,15 @@
|
||||
## 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
|
||||
<div align="center"> <img src="pics/dp-mediator.png" width="760px"/> </div><br>
|
||||
|
||||
### 实现
|
||||
Alarm(闹钟)、CoffeePot(咖啡壶)、Calendar(日历)、Sprinkler(喷头)是一组相关的对象,在某个对象的事件产生时需要去操作其它对象,形成了下面这种依赖结构:
|
||||
|
||||
<div align="center"> <img src="pics/mediator-before.png" width="640px"/> </div><br>
|
||||
@@ -154,17 +149,21 @@ public class Client {
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
```text
|
||||
doAlarm()
|
||||
doCoffeePot()
|
||||
doCalender()
|
||||
doSprinkler()
|
||||
```
|
||||
|
||||
### JDK
|
||||
|
||||
### 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...-)
|
||||
|
||||
## 参考资料
|
||||
|
||||
- 弗里曼. Head First 设计模式 [M]. 中国电力出版社, 2007.
|
||||
- Gamma E. 设计模式: 可复用面向对象软件的基础 [M]. 机械工业出版社, 2007.
|
||||
|
||||
Reference in New Issue
Block a user