diff --git a/notes/pics/observer-intent.png b/notes/pics/observer-intent.png new file mode 100644 index 00000000..2e123470 Binary files /dev/null and b/notes/pics/observer-intent.png differ diff --git a/notes/pics/observer-run.png b/notes/pics/observer-run.png new file mode 100644 index 00000000..5653ecdb Binary files /dev/null and b/notes/pics/observer-run.png differ diff --git a/notes/设计模式 - 观察者.md b/notes/设计模式 - 观察者.md index 141c513c..4cc5f65f 100644 --- a/notes/设计模式 - 观察者.md +++ b/notes/设计模式 - 观察者.md @@ -6,7 +6,7 @@ 主题(Subject)是被观察的对象,而其所有依赖者(Observer)称为观察者。 -

+

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

+

```java public interface Subject { diff --git a/tools/diagram/src/observer-intent.svg b/tools/diagram/src/observer-intent.svg new file mode 100644 index 00000000..f3f14cf8 --- /dev/null +++ b/tools/diagram/src/observer-intent.svg @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + 观察者:一个主题,多个观察者 + + WeatherData(主题) + + registerObserver(o) + removeObserver(o) + notifyObserver() + observers = [布告板 1, 布告板 2] + + CurrentConditionsDisplay + + update(temp, humidity, pressure) + + + StatisticsDisplay + + update(temp, humidity, pressure) + + + 以后新增的布告板 + + update(temp, humidity, pressure) + + 状态改变时逐个调用 update + 主题只依赖 Observer 接口,不知道具体有哪些布告板;新增布告板时只需要调用 registerObserver 注册,主题的代码不用改。 + diff --git a/tools/diagram/src/observer-run.svg b/tools/diagram/src/observer-run.svg new file mode 100644 index 00000000..f8941114 --- /dev/null +++ b/tools/diagram/src/observer-run.svg @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + setMeasurements(1, 1, 1) 之后发生了什么 + + WeatherStation + + + WeatherData + + + CurrentConditionsDisplay + + + StatisticsDisplay + + + setMeasurements(1, 1, 1) + + notifyObserver() + + update(1.0, 1.0, 1.0) + + update(1.0, 1.0, 1.0) + 实际输出:CurrentConditionsDisplay.update: 1.0 1.0 1.0,接着是 StatisticsDisplay.update: 1.0 1.0 1.0,顺序与注册的顺序相同。 +