memento模式

参考资料


• 维基百科: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模式简介


       GoFWithout violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.

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

 

      WikipediaThe 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[1]);

    return 1;
}

 

posted @ 2015-11-30 22:38  碎语心弦  阅读(646)  评论(0编辑  收藏  举报