Update 设计模式 - 备忘录.md

小节标题改为中文,重新绘制类图,补充参考资料。
This commit is contained in:
CyC2018
2026-10-28 03:19:57 +08:00
parent d862015399
commit 77ccb0dbf9
2 changed files with 11 additions and 10 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 292 KiB

+11 -10
View File
@@ -1,19 +1,16 @@
## 备忘录(Memento)
### Intent
### 意图
在不违反封装的情况下获得对象的内部状态,从而在需要时可以将对象恢复到最初状态。
### Class Diagram
### 类图
- Originator:原始对象
- Caretaker:负责保存好备忘录
- Memento:备忘录,存储原始对象的状态。备忘录实际上有两个接口,一个是提供给 Caretaker 的窄接口:它只能将备忘录传递给其它对象;一个是提供给 Originator 的宽接口,允许它访问到先前状态所需的所有数据。理想情况是只允许 Originator 访问本备忘录的内部状态。
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/50678f34-694f-45a4-91c6-34d985c83fee.png"/> </div><br>
### Implementation
<div align="center"> <img src="pics/dp-memento.png" width="760px"/> </div><br>
### 实现
以下实现了一个简单计算器程序,可以输入两个值,然后计算这两个值的和。备忘录模式允许将这两个值存储起来,然后在某个时刻用存储的状态进行恢复。
实现参考:[Memento Pattern - Calculator Example - Java Sourcecode](https://www.oodesign.com/memento-pattern-calculator-example-java-sourcecode.html)
@@ -165,12 +162,16 @@ public class Client {
}
```
```html
```text
110
-273
110
```
### JDK
### JDK 中的应用
- java.io.Serializable
## 参考资料
- 弗里曼. Head First 设计模式 [M]. 中国电力出版社, 2007.
- Gamma E. 设计模式: 可复用面向对象软件的基础 [M]. 机械工业出版社, 2007.