mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-30 18:23:52 +00:00
Update 设计模式 - 观察者.md
重新绘制配图。
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 146 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 114 KiB |
+2
-2
@@ -6,7 +6,7 @@
|
||||
|
||||
主题(Subject)是被观察的对象,而其所有依赖者(Observer)称为观察者。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/7a3c6a30-c735-4edb-8115-337288a4f0f2.jpg" width="600"/> </div><br>
|
||||
<div align="center"> <img src="pics/observer-intent.png" width="740px"/> </div><br>
|
||||
|
||||
### Class Diagram
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
天气数据布告板会在天气信息发生改变时更新其内容,布告板有多个,并且在将来会继续增加。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/b1df9732-86ce-4d69-9f06-fba1db7b3b5a.jpg"/> </div><br>
|
||||
<div align="center"> <img src="pics/observer-run.png" width="740px"/> </div><br>
|
||||
|
||||
```java
|
||||
public interface Subject {
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="740" height="290" viewBox="0 0 740 290">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="370" y="28" class="title">观察者:一个主题,多个观察者</text>
|
||||
<rect x="40" y="80" width="220" height="82" rx="4" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="150" y="98" class="c sub b" style="fill:#444">WeatherData(主题)</text>
|
||||
<line x1="40" y1="106" x2="260" y2="106" stroke="#7DBCB7"/>
|
||||
<text x="48" y="122" class="sub mono" style="fill:#444">registerObserver(o)</text>
|
||||
<text x="48" y="140" class="sub mono" style="fill:#444">removeObserver(o)</text>
|
||||
<text x="48" y="158" class="sub mono" style="fill:#444">notifyObserver()</text>
|
||||
<text x="150" y="186" class="c sub mono">observers = [布告板 1, 布告板 2]</text>
|
||||
<rect x="450" y="56" width="250" height="46" rx="4" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="575" y="74" class="c sub b" style="fill:#444">CurrentConditionsDisplay</text>
|
||||
<line x1="450" y1="82" x2="700" y2="82" stroke="#7FB2D6"/>
|
||||
<text x="458" y="98" class="sub mono" style="fill:#444">update(temp, humidity, pressure)</text>
|
||||
<line x1="262" y1="120" x2="446" y2="82" class="arrow "/>
|
||||
<rect x="450" y="126" width="250" height="46" rx="4" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="575" y="144" class="c sub b" style="fill:#444">StatisticsDisplay</text>
|
||||
<line x1="450" y1="152" x2="700" y2="152" stroke="#7FB2D6"/>
|
||||
<text x="458" y="168" class="sub mono" style="fill:#444">update(temp, humidity, pressure)</text>
|
||||
<line x1="262" y1="120" x2="446" y2="152" class="arrow "/>
|
||||
<rect x="450" y="196" width="250" height="46" rx="4" fill="#FFFFFF" stroke="#BBBBBB"/>
|
||||
<text x="575" y="214" class="c sub b" style="fill:#444">以后新增的布告板</text>
|
||||
<line x1="450" y1="222" x2="700" y2="222" stroke="#BBBBBB"/>
|
||||
<text x="458" y="238" class="sub mono" style="fill:#444">update(temp, humidity, pressure)</text>
|
||||
<line x1="262" y1="120" x2="446" y2="222" class="arrow dash"/>
|
||||
<text x="354" y="76" class="c sub" style="fill:#296898">状态改变时逐个调用 update</text>
|
||||
<text x="30" y="262" class="sub">主题只依赖 Observer 接口,不知道具体有哪些布告板;新增布告板时只需要调用 registerObserver 注册,主题的代码不用改。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.7 KiB |
@@ -0,0 +1,55 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="740" height="300" viewBox="0 0 740 300">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="370" y="28" class="title">setMeasurements(1, 1, 1) 之后发生了什么</text>
|
||||
<rect x="30" y="46" width="160" height="30" rx="6" fill="#EAE9E7" stroke="#BFBEBA"/>
|
||||
<text x="110" y="65.5" class="c sub b">WeatherStation</text>
|
||||
<line x1="110" y1="78" x2="110" y2="250" stroke="#BBB" stroke-dasharray="4 4"/>
|
||||
<rect x="220" y="46" width="160" height="30" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="300" y="65.5" class="c sub b">WeatherData</text>
|
||||
<line x1="300" y1="78" x2="300" y2="250" stroke="#BBB" stroke-dasharray="4 4"/>
|
||||
<rect x="400" y="46" width="160" height="30" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="480" y="65.5" class="c sub b">CurrentConditionsDisplay</text>
|
||||
<line x1="480" y1="78" x2="480" y2="250" stroke="#BBB" stroke-dasharray="4 4"/>
|
||||
<rect x="570" y="46" width="160" height="30" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="650" y="65.5" class="c sub b">StatisticsDisplay</text>
|
||||
<line x1="650" y1="78" x2="650" y2="250" stroke="#BBB" stroke-dasharray="4 4"/>
|
||||
<line x1="110" y1="104" x2="300" y2="104" class="arrow "/>
|
||||
<text x="205" y="98" class="c sub mono" style="fill:#444">setMeasurements(1, 1, 1)</text>
|
||||
<path d="M300,126 h26 v18 h-24" class="arrow"/>
|
||||
<text x="332" y="140" class="sub mono" style="fill:#444">notifyObserver()</text>
|
||||
<line x1="300" y1="170" x2="480" y2="170" class="arrow "/>
|
||||
<text x="390" y="164" class="c sub mono" style="fill:#444">update(1.0, 1.0, 1.0)</text>
|
||||
<line x1="300" y1="210" x2="650" y2="210" class="arrow "/>
|
||||
<text x="515" y="204" class="c sub mono" style="fill:#444">update(1.0, 1.0, 1.0)</text>
|
||||
<text x="30" y="276" class="sub">实际输出:CurrentConditionsDisplay.update: 1.0 1.0 1.0,接着是 StatisticsDisplay.update: 1.0 1.0 1.0,顺序与注册的顺序相同。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.3 KiB |
Reference in New Issue
Block a user