C++設計模式淺識備忘錄模式

來源:互聯網
上載者:User
備忘錄模式(Memento):在不破壞封裝性的前提下,捕獲一個對象的內部狀態,並在該對象之外儲存這個狀態。這樣以後就可將該對象恢複到原先儲存的狀態。

模式實現:

[code]struct State{    wchar_t wcsState[260];};class Memento{public:    Memento(State *pState): m_pState(pState) {}    State *GetState() { return m_pState; }private:    friend class Originator;    State *m_pState;};class Originator{public:    Originator() : m_pState(NULL) {}    ~Originator(){        //Delete the storage of the state        if(m_pState){            delete m_pState;            m_pState = NULL;        }    }    void SetMemento(Memento *pMemento);    Memento * CreateMemento();    void SetValue(wchar_t *val){        memset(wcsValue, 0, 260 * sizeof(wchar_t));        wcscpy_s(wcsValue, 260, val);    }    void PrintState() { std::wcout << wcsValue << std::endl; } private:    State *m_pState; //To store the object's state    wchar_t wcsValue[260]; //This is the object's real data};Memento *Originator::CreateMemento(){    m_pState = new State;    if(m_pState == NULL)        return NULL;    Memento *pMemento = new Memento(m_pState);    wcscpy_s(m_pState->wcsState, 260, wcsValue);  //Backup the value    return pMemento;}void Originator::SetMemento(Memento *pMemento){    m_pState = pMemento->GetState();    //Recovery the data    memset(wcsValue, 0, 260 * sizeof(wchar_t));    wcscpy_s(wcsValue, 260, m_pState->wcsState);}//Manager the Mementoclass Caretaker{public:    Memento *GetMemento() { return m_pMemento; }    void SetMemento(Memento *pMemento){        //Free the previous Memento        if(m_pMemento){            delete m_pMemento;            m_pMemento = NULL;        }        //set the new Memento        m_pMemento = pMemento;    }private:    Memento *m_pMemento;};

用戶端:

[code]int main(){     Originator *pOriginator = new Originator();    pOriginator->SetValue(L"on");    pOriginator->PrintState();  //OutPut: on    //Now I backup the state    Caretaker *pCaretaker = new Caretaker();    pCaretaker->SetMemento(pOriginator->CreateMemento());    //Set the new state    pOriginator->SetValue(L"off");    pOriginator->PrintState(); //OutPut: off    //Recovery to the old state    pOriginator->SetMemento(pCaretaker->GetMemento());    pOriginator->PrintState(); //OutPut: on    if(pCaretaker)        delete pCaretaker;    if(pOriginator);        delete pOriginator;    return 0;}

以上就是C++設計模式淺識備忘錄模式的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.