[Design mode] memento memorandum Mode

Source: Internet
Author: User
Problem

No one wants to make mistakes, but no one can make mistakes. A mistake can only be corrected, but it is difficult to correct (recover ). There is no regret in the world, but we have to give users the right to regret when designing software systems (in fact, it may also be the right requested by users :)), we certainly need to provide undo operations for some key operations. The regret medicine is provided by the memento mode.

Memento memorandum Mode

Capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the Saved state.

Resolution: In memento mode, the state to be saved is encapsulated and retrieved for recovery only when it is restored. the principle is very simple. Pay attention to the narrow interface and wide interface. the so-called wide interface is an interface in the general sense. It uses an external interface as a public member. The narrow interface, on the contrary, uses the interface as a private member, the class that needs to access these interface functions is used as the friend class of this class, that is, the interface is only exposed to the class that is interested in these interfaces, rather than exposed to the outside. the following implementation is implemented using a narrow implementation method.

Demo

Memento. h

# Ifndef MEMENTO_H # define MEMENTO_H # include <string> typedef std: string State; class Memento; class Originator {public: Originator (const State & rState); Originator (){}~ Originator () {} Memento * createmento (); memory (Memento * pMemento); StateGetState (); voidSetState (const State & rState); voidRestoreState (Memento * pMemento ); voidPrintState (); private: Statem_State;}; // set all Memento interface functions to private, while Originator is its friend, // This ensures that only Originator can access class Memento {private: friend class Originator; Memento (const State & rState); voidSetState (const State & rState); StateGetState (); statem_State ;};# endif

 

Memento. cpp

#include "Memento.h"#include <iostream> Originator::Originator(const State& rState) : m_State(rState){} State Originator::GetState(){return m_State;}void Originator::SetState(const State& rState){m_State = rState;}Memento* Originator::CreateMemento(){return new Memento(m_State);}void Originator::RestoreState(Memento* pMemento){if (NULL != pMemento){m_State = pMemento->GetState();}}void Originator::PrintState(){std::cout << "State = " << m_State << std::endl;}Memento::Memento(const State& rState) : m_State(rState){}State Memento::GetState(){return m_State;}void Memento::SetState(const State& rState){m_State = rState;}

 

Main. cpp

# Include "Memento. h "# include <stdlib. h> int main () {// create an inator * pOriginator = new Originator ("old state"); pOriginator-> PrintState (); // create a memorandum to store the state Memento * pMemento = pOriginator-> CreateMemento (); // change the state of the original object pOriginator-> SetState ("new state "); pOriginator-> PrintState (); // restores the status of the primary machine to the previous state pOriginator-> RestoreState (pMemento); pOriginator-> PrintState (); delete pOriginator; delete pMemento; system ("pause"); return 0 ;}

Code Description: The Key to Memento mode is the friend class Originator. We can see that all Memento interfaces are declared as private, and Originator is declared as the Memento membership class. We save the Originator status in the Memento class, and private the Memento interface, which achieves the encapsulation function.

In the Originator class, we provide methods to make users regret: RestoreToMemento (Memento * mt); we can use this interface to make users regret. In the test program, we demonstrated this point: the Originator state changes from old to new and returns to old.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.