💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
# 已知模式-备忘录模式(Memento) ## **模式的定义与特点** 备忘录(Memento)模式的定义:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先保存的状态。该模式又叫快照模式。 > 很多应用软件都提供了这项功能,如 Word、记事本、Photoshop、Eclipse 等软件在编辑时按 Ctrl+Z 组合键时能撤销当前操作,使文档恢复到之前的状态;还有在 IE 中的后退键、数据库事务管理中的回滚操作、玩游戏时的中间结果存档功能、数据库与操作系统的备份操作、棋类游戏中的悔棋功能等都属于这类 **优点:** - 提供了一种可以恢复状态的机制。当用户需要时能够比较方便地将数据恢复到某个历史的状态。 - 实现了内部状态的封装。除了创建它的发起人之外,其他对象都不能够访问这些状态信息。 - 简化了发起人类。发起人不需要管理和保存其内部状态的各个备份,所有状态信息都保存在备忘录中,并由管理者进行管理,这符合单一职责原则。 **缺点:** - 资源消耗大。如果要保存的内部状态信息过多或者特别频繁,将会占用比较大的内存资源。 ## **模式的应用场景** 1. 需要保存与恢复数据的场景,如玩游戏时的中间结果的存档功能。 2. 需要提供一个可回滚操作的场景,如 Word、记事本、Photoshop,Eclipse 等软件在编辑时按 Ctrl+Z 组合键,还有数据库中事务操作。 ## **模式的结构** 备忘录模式的核心是设计**备忘录类**以及用于**管理备忘录的管理者类** 1. 发起人(Originator)角色:记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,实现其他业务功能,它可以访问备忘录里的所有信息。 2. 备忘录(Memento)角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人。 3. 管理者(Caretaker)角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。 备忘录模式的结构图如图 ![](https://img.kancloud.cn/66/22/66229341ef45799d24331b9b0611c1a7_568x365.png) ``` <pre class="calibre10">``` <span class="token">//备忘录 : 负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人</span> class <span class="token4">Memento</span> <span class="token3">{</span> private $state<span class="token1">=</span><span class="token2">""</span><span class="token3">;</span> <span class="token">/** * 实例设置初始状态 * @param String $state */</span> public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>String $state<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>state<span class="token1">=</span>$state<span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 记录状态 * @param String $state */</span> public <span class="token5">function</span> <span class="token4">setState</span><span class="token3">(</span>String $state<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>state<span class="token1">=</span>$state<span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 获取状态 * @return string */</span> public <span class="token5">function</span> <span class="token4">getState</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>state<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//发起人: 记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,实现其他业务功能,它可以访问备忘录里的所有信息</span> class <span class="token4">Originator</span> <span class="token3">{</span> private $state<span class="token1">=</span><span class="token2">""</span><span class="token3">;</span> public <span class="token5">function</span> <span class="token4">setState</span><span class="token3">(</span>String $state<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>state<span class="token1">=</span>$state<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">getState</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>state<span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 创建备忘录 * @return Memento 返回备忘录类实例 */</span> public <span class="token5">function</span> <span class="token4">createMemento</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">Memento</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>state<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 恢复备忘录中记录的状态 * @param Memento $m 备忘录实例 * @return string */</span> public <span class="token5">function</span> <span class="token4">restoreMemento</span><span class="token3">(</span>Memento $m<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span><span class="token4">setState</span><span class="token3">(</span>$m<span class="token1">-</span><span class="token1">></span><span class="token4">getState</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//管理者: 对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改</span> class <span class="token4">Caretaker</span> <span class="token3">{</span> private $memento<span class="token3">;</span> <span class="token">/** * 保存备忘录实例 * @param Memento $m 备忘录实例 */</span> public <span class="token5">function</span> <span class="token4">setMemento</span><span class="token3">(</span>Memento $m<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>memento<span class="token1">=</span>$m<span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 获取备忘录实例 * @return Memento */</span> public <span class="token5">function</span> <span class="token4">getMemento</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>memento<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> class <span class="token4">MementoPattern</span> <span class="token3">{</span> public static <span class="token5">function</span> <span class="token4">main</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">//发起人</span> $or<span class="token1">=</span><span class="token5">new</span> <span class="token4">Originator</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//管理者</span> $cr<span class="token1">=</span><span class="token5">new</span> <span class="token4">Caretaker</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//发起人设置状态</span> $or<span class="token1">-</span><span class="token1">></span><span class="token4">setState</span><span class="token3">(</span><span class="token2">"s0"</span><span class="token3">)</span><span class="token3">;</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"初始状态:"</span><span class="token3">.</span>$or<span class="token1">-</span><span class="token1">></span><span class="token4">getState</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//管理者 将发起人创建的备忘录保存起来</span> $cr<span class="token1">-</span><span class="token1">></span><span class="token4">setMemento</span><span class="token3">(</span>$or<span class="token1">-</span><span class="token1">></span><span class="token4">createMemento</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//保存状态 </span> $or<span class="token1">-</span><span class="token1">></span><span class="token4">setState</span><span class="token3">(</span><span class="token2">"S1"</span><span class="token3">)</span><span class="token3">;</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"新的状态:"</span><span class="token3">.</span>$or<span class="token1">-</span><span class="token1">></span><span class="token4">getState</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> $or<span class="token1">-</span><span class="token1">></span><span class="token4">restoreMemento</span><span class="token3">(</span>$cr<span class="token1">-</span><span class="token1">></span><span class="token4">getMemento</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//恢复状态</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"恢复状态:"</span><span class="token3">.</span>$or<span class="token1">-</span><span class="token1">></span><span class="token4">getState</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> MementoPattern<span class="token3">:</span><span class="token3">:</span><span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` ## 模式的扩展 在前面介绍的备忘录模式中,有单状态备份的例子,也有多状态备份的例子。 下面介绍备忘录模式如何同[原型模式](http://c.biancheng.net/view/1343.html)混合使用。在备忘录模式中,通过定义“备忘录”来备份“发起人”的信息,而原型模式的 clone() 方法具有自备份功能,所以,如果让发起人实现 Cloneable 接口就有备份自己的功能,这时可以删除备忘录类,其结构图如图 所示。 ![](https://img.kancloud.cn/94/01/9401805ef401f4fa0384177e61fa24c0_787x420.png) ``` <pre class="calibre17">``` <span class="token">//声明一个克隆自身的接口 </span> interface <span class="token4">Cloneable</span> <span class="token3">{</span> <span class="token5">function</span> <span class="token4">clone</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">//发起人原型</span> class <span class="token4">OriginatorPrototype</span> implements <span class="token4">Cloneable</span><span class="token3">{</span> private $state<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">setState</span><span class="token3">(</span>String $state<span class="token3">)</span><span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>state<span class="token1">=</span>$state<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">getState</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>state<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">createMemento</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span><span class="token4">clone</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">restoreMemento</span><span class="token3">(</span>OriginatorPrototype $opt<span class="token3">)</span><span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span><span class="token4">setState</span><span class="token3">(</span>$opt<span class="token1">-</span><span class="token1">></span><span class="token4">getState</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">clone</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> clone $this<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//原型管理者</span> class <span class="token4">PrototypeCaretaker</span> <span class="token3">{</span> private $opt<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">setMemento</span><span class="token3">(</span>OriginatorPrototype $opt<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>opt<span class="token1">=</span>$opt<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">getMemento</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>opt<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> $originatorPrototype<span class="token1">=</span><span class="token5">new</span> <span class="token4">OriginatorPrototype</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $prototypeCaretaker<span class="token1">=</span><span class="token5">new</span> <span class="token4">PrototypeCaretaker</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//发起人改变状态</span> $originatorPrototype<span class="token1">-</span><span class="token1">></span><span class="token4">setState</span><span class="token3">(</span><span class="token2">"s0"</span><span class="token3">)</span><span class="token3">;</span> echo <span class="token2">"初始状态:"</span><span class="token3">.</span>$originatorPrototype<span class="token1">-</span><span class="token1">></span><span class="token4">getState</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//管理者保存设置好以后的状态(将发起者类实例存入)</span> $prototypeCaretaker<span class="token1">-</span><span class="token1">></span><span class="token4">setMemento</span><span class="token3">(</span>$originatorPrototype<span class="token1">-</span><span class="token1">></span><span class="token4">createMemento</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//发起人再次改变状态</span> $originatorPrototype<span class="token1">-</span><span class="token1">></span><span class="token4">setState</span><span class="token3">(</span><span class="token2">"s1"</span><span class="token3">)</span><span class="token3">;</span> echo <span class="token2">"新的状态:"</span><span class="token3">.</span>$originatorPrototype<span class="token1">-</span><span class="token1">></span><span class="token4">getState</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//发起人恢复状态</span> $originatorPrototype<span class="token1">-</span><span class="token1">></span><span class="token4">restoreMemento</span><span class="token3">(</span>$prototypeCaretaker<span class="token1">-</span><span class="token1">></span><span class="token4">getMemento</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> echo <span class="token2">"恢复状态:"</span><span class="token3">.</span>$originatorPrototype<span class="token1">-</span><span class="token1">></span><span class="token4">getState</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ```