码迷,mamicode.com
首页 > 其他好文 > 详细

Memento

时间:2014-12-19 13:03:47      阅读:154      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!