memento模式
参考资料
• 维基百科:https://en.wikipedia.org/wiki/Memento_pattern
Memento模式简介
GoF:Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
GoF:在不破坏封闭的前提下,捕获并外部化一个对象的内部状态,这样之后就可以将该对象恢复到保存时的状态。
Wikipedia:The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback). The memento pattern is implemented with three objects: the originator, a caretaker and a memento. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot, or should not, change). When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object.
Wikipedia:备忘录模式是一种软件设计模式,它提供一种能将一个对象恢复到旧状态的能力(回滚式的撤销操作)。备忘录模式通过三个对象来实现:originator、caretaker和memento。originator是一些拥有内部状态的对象。caretaker将在originator之上进行一些处理,但是同时希望能够撤销之前的处理操作。caretaker首先向originator请求一个memento对象,然后它将进行它所要进行的任何操作。为了回滚回进行这些操作的状态,caretaker将返回memento对象给originator。memento对象是一种不透明对象(caretaker不能也不应该对其改变)。使用这种模式的时候,需要注意的是originator可能改变其他对象或资源的状态,备忘录模式只对单一对象起作用。
百度百科:在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。
Memento模式详解
• 设计意图
在不破坏封闭的前提下,捕获并外部化一个对象的内部状态,这样之后就可以将该对象恢复到保存时的状态。
• 结构图

• 结构说明
▶ Memento
备忘录,用于存储originator对象的内部状态。memento对象应该根据originator的实际需要,存储必要的originator对象的内部状态。它能够保护对状态的访问。memento实际上拥有两个接口。caretaker对象只能访问memento对象的窄接口,即它只能够把memento对象传递给其他对象。相反,originator对象可以访问memento的宽接口,即获取所有能恢复到旧状态的必要数据。理想情况下,只有创建memento的oiginator对象才有权限访问memento对象的内部状态信息.
▶ Originator
发起人。创建一个memento对象来存储它当前内部状态的一个快照,这些内部信息将保存在memento对象之中。
▶ Caretaker
管理者,负责memento对象的安全存储,但从不对memento对象的内容进行操作或检查。
• 时序图
caretaker对象向originator对象请求并持有一个memento,然后将memento对象传递回给originator,如下图所以。有时caretaker对象并不会将memento对象传递回给originator,这是因为originator可能不需要回滚回旧状态。memento对象总是消极的,只有创建memento对象的originator对象才会分配或回复其状态。

Memento模式举例
• 例子一
这个例子出自于Wikipedia:https://en.wikipedia.org/wiki/Memento_pattern
#include <vector>
#include <string>
#include <iostream>
using namespace std; // Memento: It holds the internal state of Originator
class Memento
{
private:
string m_State; public:
Memento(const string &state = "") : m_State(state) {} public:
const string &GetState() const
{
return m_State;
} void SetState(const string& state)
{
m_State = state;
}
}; // Originator: It is the one whose state needs to be saved and creates a Memento object.
class Originator
{
private:
string m_State;
// The class could also contain additional data that is not part of the state saved in the memento.
public:
void ChangeState(const string &state)
{
cout << "Originator: Changing state to " << state << endl;
m_State = state;
} Memento SaveToMemento()
{
cout << "Originator: Saving to Memento." << endl;
return Memento(m_State);
} void RestoreFromMemento(const Memento &memento)
{
m_State = memento.GetState();
cout << "Originator: State after restoring from Memento is " << m_State << endl;
}
}; int main()
{
// Acts as Caretaker here
vector<Memento> mementos;
Originator originator; originator.ChangeState("State 1");
originator.ChangeState("State 2"); // Save "State 2" to memento
mementos.push_back(originator.SaveToMemento()); originator.ChangeState("State 3"); // Save "State 3" to memento
mementos.push_back(originator.SaveToMemento()); originator.ChangeState("State 4"); // Restore "State 3" to originator
originator.RestoreFromMemento(mementos[]); return ;
}
memento模式的更多相关文章
- Memento 模式
Memento 模式的关键就是要在不破坏封装行的前提下,捕获并保存一个类的内部状态,这样就可以利用该保存的状态实施恢复操作. /////////Originator.h//////////////// ...
- 【行为型】Memento模式
备忘录模式顾名思义就是一种能有备忘作用的设计模式,其目的是在对象外部保存其在某一时刻的状态信息,并且在任何需要的时候,都可以通过备忘录中保存的状态数据恢复对象在当时情形下的状态. 备忘录模式旨在对象的 ...
- Java设计模式(15)备忘录模式(Memento模式)
Memento定义:memento是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到原先保存的状态. Memento模式相对也比较好理解,我们看下列代码: public class ...
- 设计模式之——Memento模式
Memento模式即快照模式,就是在某一时刻,设定一个状态,在后面随时可以返回到当前状态的模式. 我们拿一个闯关游戏作为举例,一共有十关,每闯一关,玩家所持金额增加一百,而闯关失败就扣一百.初始时,给 ...
- Memento模式(备忘录设计模式)
Memento模式? 使用面向对象编程的方式实现撤销功能时,需要事先保存实例的相关状态信息.然后,在撤销时,还需要根据所保存的信息将实例恢复至原来的状态.这个时候你需要使用Memento设计模式.(以 ...
- C++设计模式实现--备忘录(Memento)模式
一. 备忘录模式 定义:在不破坏封装性的前提下,捕获一个对象的内部状态.并在该对象之外保存这个状态. 这样以后就可将该对象恢复到原先保存的状态. 结构图: 使用范围: Memento 模式比較适用于功 ...
- Behavioral模式之Memento模式
1.意图 在不破坏封装性的前提下,捕获一个对象的内部状态.并在该对象之外保存这个状态,这样以后就可将该对象恢复到原先保存的状态. 2.别名 Token 3.动机 有时候有必要记录一个对象的内部状态.为 ...
- 设计模式(十八)Memento模式
在使用面向对象编程的方式实现撤销功能时,需要事先保存实例的相关状态信息.然后,在撤销时,还需要根据所保存的信息将实例恢复至原来的状态. 要想恢复实例,需要一个可以自由访问实例内部结构的权限.但是,如果 ...
- 设计模式C++描述----17.备忘录(Memento)模式
一. 备忘录模式 定义:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 结构图: 使用范围: Memento 模式比较适用于功能 ...
随机推荐
- 为什么逻辑斯特回归(logistic regression)是线性模型
一个典型的logistic regression模型是: 这里明明用了非线性函数,那为什么logistic regression还是线性模型呢? 首先,这个函数不是f(y,x)=0的函数,判断一个模型 ...
- PyCharm中设置菜单字体大小
file——>setting,然后选择appearance,下图右侧红色边框中的内容即设置菜单的字体和大小
- could not find com.android.support.appcompat-v7:23.4.0
导入别人的工程到AS中,出现错误,是由于android studio的版本比所加载的工程所使用的版本低,有些包不是最新的. 我的android studio这个包的版本是 v7:23.1.1 所以需要 ...
- 在XML中用于注释的符号是。(选择1项)
A.<!– –> B.<?– –?> C.<% %> D.<!– –!> 解答:A
- Microsoft Word、Excel、PowerPoint转Pdf
Worksheet.ExportAsFixedFormat Method Mark: The ExportAsFixedFormat method is used to publish a workb ...
- ELK显示多行日志
1.默认,logstash对日志文件的选取是以单行为单位的:但像log4j这种输出日志经常会是以时间头开始的多行日志: 2.显示多行,需要配置logstash的config: input { file ...
- eq与gt的妙用
应用到jq中: 一.jquery :gt选择器: 定义: :gt 选择器选取 index 值高于指定数的元素. 语法:$(":gt(index)") ex:$("l ...
- 『SharePoint 2010』Sharepoint 2010 Form 身份认证的实现(基于AD)
一.进管理中心,创建一个应用程序,配置如下: 二.填端口号,和选择form身份认证,以及填写成员和角色,其他都默认就可以了 三.使用SharePoint 2010 Management Shell在里 ...
- 160229-01、web页面常用功能js实现
web页面常用功能js实现 1.网页未加载时弹出新窗口 <body onunload="window.open('http://www.a68.cn');">< ...
- 160425、linux安装SVN服务器
1:查看linux是否已经安装svn服务 [root@nb ~]# rpm -qa subversion subversion-1.6.11-15.el6_7.x86_64 2:安装svn #yum ...