diff --git a/notes/pics/dp-observer.png b/notes/pics/dp-observer.png new file mode 100644 index 00000000..e1ca6341 Binary files /dev/null and b/notes/pics/dp-observer.png differ diff --git a/notes/设计模式 - 观察者.md b/notes/设计模式 - 观察者.md index 4cc5f65f..b5453bf0 100644 --- a/notes/设计模式 - 观察者.md +++ b/notes/设计模式 - 观察者.md @@ -1,23 +1,20 @@ ## 7. 观察者(Observer) -### Intent - +### 意图 定义对象之间的一对多依赖,当一个对象状态改变时,它的所有依赖都会收到通知并且自动更新状态。 主题(Subject)是被观察的对象,而其所有依赖者(Observer)称为观察者。

-### Class Diagram - +### 类图 主题(Subject)具有注册和移除观察者、并通知所有观察者的功能,主题是通过维护一张观察者列表来实现这些操作的。 观察者(Observer)的注册功能需要调用主题的 registerObserver() 方法。 -

- -### Implementation +

+### 实现 天气数据布告板会在天气信息发生改变时更新其内容,布告板有多个,并且在将来会继续增加。

@@ -119,16 +116,20 @@ public class WeatherStation { } ``` -```html +```text CurrentConditionsDisplay.update: 0.0 0.0 0.0 StatisticsDisplay.update: 0.0 0.0 0.0 CurrentConditionsDisplay.update: 1.0 1.0 1.0 StatisticsDisplay.update: 1.0 1.0 1.0 ``` -### JDK - +### JDK 中的应用 - [java.util.Observer](http://docs.oracle.com/javase/8/docs/api/java/util/Observer.html) - [java.util.EventListener](http://docs.oracle.com/javase/8/docs/api/java/util/EventListener.html) - [javax.servlet.http.HttpSessionBindingListener](http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpSessionBindingListener.html) - [RxJava](https://github.com/ReactiveX/RxJava) + +## 参考资料 + +- 弗里曼. Head First 设计模式 [M]. 中国电力出版社, 2007. +- Gamma E. 设计模式: 可复用面向对象软件的基础 [M]. 机械工业出版社, 2007.