参考资料


• 维基百科: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

  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. // Memento: It holds the internal state of Originator
  7. class Memento
  8. {
  9. private:
  10. string m_State;
  11.  
  12. public:
  13. Memento(const string &state = "") : m_State(state) {}
  14.  
  15. public:
  16. const string &GetState() const
  17. {
  18. return m_State;
  19. }
  20.  
  21. void SetState(const string& state)
  22. {
  23. m_State = state;
  24. }
  25. };
  26.  
  27. // Originator: It is the one whose state needs to be saved and creates a Memento object.
  28. class Originator
  29. {
  30. private:
  31. string m_State;
  32. // The class could also contain additional data that is not part of the state saved in the memento.
  33. public:
  34. void ChangeState(const string &state)
  35. {
  36. cout << "Originator: Changing state to " << state << endl;
  37. m_State = state;
  38. }
  39.  
  40. Memento SaveToMemento()
  41. {
  42. cout << "Originator: Saving to Memento." << endl;
  43. return Memento(m_State);
  44. }
  45.  
  46. void RestoreFromMemento(const Memento &memento)
  47. {
  48. m_State = memento.GetState();
  49. cout << "Originator: State after restoring from Memento is " << m_State << endl;
  50. }
  51. };
  52.  
  53. int main()
  54. {
  55. // Acts as Caretaker here
  56. vector<Memento> mementos;
  57. Originator originator;
  58.  
  59. originator.ChangeState("State 1");
  60. originator.ChangeState("State 2");
  61.  
  62. // Save "State 2" to memento
  63. mementos.push_back(originator.SaveToMemento());
  64.  
  65. originator.ChangeState("State 3");
  66.  
  67. // Save "State 3" to memento
  68. mementos.push_back(originator.SaveToMemento());
  69.  
  70. originator.ChangeState("State 4");
  71.  
  72. // Restore "State 3" to originator
  73. originator.RestoreFromMemento(mementos[]);
  74.  
  75. return ;
  76. }

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. Java.io下的方法是对磁盘上的文件进行磁盘操作

    File类(java.io.*)可表示一个文件,也有可能是一个目录(在JAVA中文件和目录都属于这个类中,而且区分不是非常的明显). Java.io下的方法是对磁盘上的文件进行磁盘操作,但是无法读取文 ...

  2. 5.3 SpEL语法

    SqEL是一个可以独立于spring的表达式语言,即它可以用在XML中对语法进行简化 5.3 SpEL语法5.3.1 基本表达式一.字面量表达式: SpEL支持的字面量包括:字符串.数字类型(int. ...

  3. 使用命令行操控VirtualBox虚拟机

    (1)启动虚拟机:$ VBoxManage startvm <VMNAME> --type gui  #执行结束后,就会启动指定的虚拟机,几乎和平时没什么区别. $ VBoxManage ...

  4. ip地址查询系统和CMD查询的结果不一样

    由于cmd输入 ipconfig查看的IP是局域网内网IP,而用ip地址查看器查看是公网上网的ip地址.所以不一样. 查询内网ip: windows系统: 开始--运行--cmd,命令行输入: ipc ...

  5. python定义函数时的默认返回值

    python定义函数时,一般都会有指定返回值,如果没有显式指定返回值,那么python就会默认返回值为None, 即隐式返回语句: return None 执行如下代码 def now(): prin ...

  6. 20 个常用的 CSS 技巧

    1. 黑白图像 这段代码会让你的彩色照片显示为黑白照片,是不是很酷? img.desaturate {    filter: grayscale(100%);    -webkit-filter: g ...

  7. Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication 解决办法

    相信很多人都遇到过这个问题,用Android Studio正在运行程序的时候,突然不知道什么原因,报一个找不到application或者找不到activity的错误(java.lang.ClassNo ...

  8. 链接href的多重使用

    1. 拨打电话 在电话号码前面可以加上 + (加号)表示国际号码. <a href="tel:10086">10086</a> 使用wtai协议进行拨打电话 ...

  9. Linux上查看和修改字符集

    author :headsen chen date: 2018-05-14  16:20:30 一·查看字符集 字符集在系统中体现形式是一个环境变量,看当前终端使用字符集的有以下几种方式: 1: 1 ...

  10. node 同异步处理

    同步:序列执行,需等待 异步:非序列执行,无需等待 node同步处理:读取->输出->完毕(队列式执行) node异步处理:读取->完毕(回调输出)(后两步同时进行,谁先到谁先输出) ...