标签:
#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; }
标签:
原文地址:http://www.cnblogs.com/stanley198610281217/p/4173626.html