参考资料


• 维基百科:https://en.wikipedia.org/wiki/Memento_pattern

• 百度百科:http://baike.baidu.com/link?url=ZQZv4Uv7uXtgITrmTfvdS4Kl-lANTfqfqZUCGAQgqFF1pTlOBIo3Ka3or_FKGRoFP5ljPvjawk2nu2Bpd-Asm6dUxHZCN4yxsC2cA6zHtDG

http://baike.baidu.com/link?url=id-CLN_Iq7xrHrmqMPr4VKytEfkya1ai0jz_5vOwD-epzCvSk-H4-Smif174iWROz_Vc4mZlpMl1G5fPXL85WK

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:备忘录模式是一种软件设计模式,它提供一种能将一个对象恢复到旧状态的能力(回滚式的撤销操作)。备忘录模式通过三个对象来实现:originatorcaretakermementooriginator是一些拥有内部状态的对象。caretaker将在originator之上进行一些处理,但是同时希望能够撤销之前的处理操作。caretaker首先向originator请求一个memento对象,然后它将进行它所要进行的任何操作。为了回滚回进行这些操作的状态,caretaker将返回memento对象给originatormemento对象是一种不透明对象(caretaker不能也不应该对其改变)。使用这种模式的时候,需要注意的是originator可能改变其他对象或资源的状态,备忘录模式只对单一对象起作用。

百度百科:在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。

Memento模式详解


• 设计意图

在不破坏封闭的前提下,捕获并外部化一个对象的内部状态,这样之后就可以将该对象恢复到保存时的状态。

• 结构图

• 结构说明

▶ Memento

备忘录,用于存储originator对象的内部状态。memento对象应该根据originator的实际需要,存储必要的originator对象的内部状态。它能够保护对状态的访问。memento实际上拥有两个接口。caretaker对象只能访问memento对象的窄接口,即它只能够把memento对象传递给其他对象。相反,originator对象可以访问memento的宽接口,即获取所有能恢复到旧状态的必要数据。理想情况下,只有创建mementooiginator对象才有权限访问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模式的更多相关文章

  1. Memento 模式

    Memento 模式的关键就是要在不破坏封装行的前提下,捕获并保存一个类的内部状态,这样就可以利用该保存的状态实施恢复操作. /////////Originator.h//////////////// ...

  2. 【行为型】Memento模式

    备忘录模式顾名思义就是一种能有备忘作用的设计模式,其目的是在对象外部保存其在某一时刻的状态信息,并且在任何需要的时候,都可以通过备忘录中保存的状态数据恢复对象在当时情形下的状态. 备忘录模式旨在对象的 ...

  3. Java设计模式(15)备忘录模式(Memento模式)

    Memento定义:memento是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到原先保存的状态. Memento模式相对也比较好理解,我们看下列代码: public class ...

  4. 设计模式之——Memento模式

    Memento模式即快照模式,就是在某一时刻,设定一个状态,在后面随时可以返回到当前状态的模式. 我们拿一个闯关游戏作为举例,一共有十关,每闯一关,玩家所持金额增加一百,而闯关失败就扣一百.初始时,给 ...

  5. Memento模式(备忘录设计模式)

    Memento模式? 使用面向对象编程的方式实现撤销功能时,需要事先保存实例的相关状态信息.然后,在撤销时,还需要根据所保存的信息将实例恢复至原来的状态.这个时候你需要使用Memento设计模式.(以 ...

  6. C++设计模式实现--备忘录(Memento)模式

    一. 备忘录模式 定义:在不破坏封装性的前提下,捕获一个对象的内部状态.并在该对象之外保存这个状态. 这样以后就可将该对象恢复到原先保存的状态. 结构图: 使用范围: Memento 模式比較适用于功 ...

  7. Behavioral模式之Memento模式

    1.意图 在不破坏封装性的前提下,捕获一个对象的内部状态.并在该对象之外保存这个状态,这样以后就可将该对象恢复到原先保存的状态. 2.别名 Token 3.动机 有时候有必要记录一个对象的内部状态.为 ...

  8. 设计模式(十八)Memento模式

    在使用面向对象编程的方式实现撤销功能时,需要事先保存实例的相关状态信息.然后,在撤销时,还需要根据所保存的信息将实例恢复至原来的状态. 要想恢复实例,需要一个可以自由访问实例内部结构的权限.但是,如果 ...

  9. 设计模式C++描述----17.备忘录(Memento)模式

    一. 备忘录模式 定义:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 结构图: 使用范围: Memento 模式比较适用于功能 ...

随机推荐

  1. 如果想要跨平台,在file类下有separtor(),返回锁出平台的文件分隔符

    对于命令:File f2=new file(“d:\\abc\\789\\1.txt”) 这个命令不具备跨平台性,因为不同的OS的文件系统很不相同. 如果想要跨平台,在file类下有separtor( ...

  2. 辛星和您一起解析PHP中的单例模式

    事实上单例模式还是用的挺多的,要说到最经典的样例.可能就是操纵数据库的类了,它假设是单例的话,能够避免大量的new操作消耗资源,而假设系统中须要一个类来管理全局的信息,则把它用成单例也是非常不错的.由 ...

  3. 在其模块列表中有一个错误模块“ManagedPipelineHandler”。

    C:\Windows\Microsoft.NET\Framework\v4.0.30319 命令行: aspnet_regiis -i

  4. 转载 -- iOS中SDK的简单封装与使用

    一.功能总述 在博客开始的第一部分,我们先来看一下我们最终要实现的效果.下图中所表述的就是我们今天博客中要做的事情,下方的App One和App Two都植入了我们将要封装的LoginSDK, 两个A ...

  5. JQuery------图片幻灯片插件

    下载地址: http://www.jq22.com/jquery-info36

  6. Spring_day04--SSH框架整合过程

    SSH框架整合过程 第一步 导入jar包 第二步 搭建struts2环境 (1)创建action,创建struts.xml配置文件,配置action (2)配置struts2的过滤器 第三步 搭建hi ...

  7. Hadoop1.2.1 配置文件详解

    首先我们先回顾一下Hadoop的一些概念: Apache Hdoop 1.x 组成 NameNode(元数据服务器) Secondary NameNode(辅助元数据服务器) JobTracker(任 ...

  8. Securi-Pi:使用树莓派作为安全跳板

    导读 像很多 LinuxJournal 的读者一样,我也过上了当今非常普遍的“科技游牧”生活,在网络之间,从一个接入点到另一个接入点,我们身处现实世界的不同地方却始终保持连接到互联网和日常使用的其它网 ...

  9. 获取Asset下文本内容和读取图片

    import android.content.Context; import android.content.res.AssetManager; import android.graphics.Bit ...

  10. 合并图片、EUI顺序、CacheAsBitmap对drawcall影响的测试

    一 什么是DrawCall Draw Call 理解和优化: http://blog.csdn.net/sakyaer/article/details/44459881 draw call是openG ...