首页 > 其他 > 详细

Memento

时间:2014-12-19 13:02:42      阅读:243      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <string>

using namespace std;


class Memento
{
public:
    Memento(string sState) { m_sState = sState; }
    
    string GetState() { return m_sState; }
    void SetState(string sState) { m_sState = sState; }
    
private:
    string m_sState;
};

class Originator
{
public:
    Originator() : m_memento("") {}
    
    void Executor(string sDo)
    {
        cout<<sDo<<endl;
    }

    void SetSate(const Memento& memento) { m_memento = memento; cout<<"current state:"<<m_memento.GetState()<<endl; }
    Memento CurrentState()
    {
        return m_memento;
    }

private:
    Memento m_memento;
};



class Caretaker
{
public:
    Caretaker() : m_backupMemento("") { m_executor.SetSate(Memento("Initial state")); }
    
    void Begin()
    {
        m_backupMemento = m_executor.CurrentState();
        m_executor.SetSate(Memento("ready for transaction"));
        m_executor.Executor("begin transaction...");
    }
        
    void Rollback()
    {
        m_executor.SetSate(Memento("rollback transaction"));
        m_executor.Executor("rollback...");
        m_executor.SetSate(m_backupMemento);
    }
    
private:
    Originator m_executor;
    Memento m_backupMemento;
};



int main(int argc, char *argv[])
{
    Caretaker caretaker;
    caretaker.Begin();
    caretaker.Rollback();

    return 0;
}

 

Memento

原文:http://www.cnblogs.com/stanley198610281217/p/4173626.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!