Memo pattern parsing in design pattern and relevant C + + instance application _c language

Source: Internet
Author: User

The memo mode is designed to capture the internal state of an object without damaging the encapsulation, and to save the state outside the object. The object can then be restored to its previously saved state. In command mode, the memo pattern is often used to maintain the state of the undo operation.

Class Diagram:

    • Originator: is responsible for creating a memo memento to record its internal state at the current moment, and to restore the internal state using the memo. Originator can determine which internal state of the memento storage originator as needed.
    • Memento: is responsible for storing the internal state of originator objects and prevents access to the memo Memento to objects other than originator. The memo has two interfaces, caretaker can only see the narrow interface of the memo, it can only pass the memo to other objects. Originator can see a wide interface that allows it to access all the data needed to return to the previous state.
    • Caretaker: Responsible for keeping the memo memento, can not operate or check the contents of the memo.

The memento mode encapsulates the state that needs to be saved, when recovery is needed, it is removed for recovery. The principle is very simple, the realization of the time need to pay attention to a place: narrow interface and wide interface. The so-called wide interface is the general interface, the external interface as a public member, and the narrow interface instead, Use the interface as a private member, and the class that needs access to these interface functions as a friend of the class, that is, the interface is exposed only to classes that are interested in those interfaces, rather than exposing the outer part. The following implementations are implemented in a narrow implementation approach.

Memento mode is useful for complex functions, but the classes that need to maintain or record historical attributes, or the properties that need to be saved are only a small part of many properties, originator can revert to the previous state based on the saved memento information.

If you need to implement the command revocation function when using command mode in a system, the command mode can use Memo mode to store the status of the revocable operation.
Example:

#include <iostream> #include <string> using namespace std; 
 
Class Memento {private:string state; 
  Public:memento (String state): state {} string GetState () {return state; 
  } void SetState (string state) {this->state = state; 
 
} 
}; 
 
Class Caretaker {Private:memento *memento; 
  Public:void Setmemento (Memento *memento) {this->memento = Memento; 
  } memento* Getmemento () {return this->memento; 
 
} 
}; 
   
Class Originator {private:string state; 
  Public:originator (string state) {this->state = state; 
  } void Restorememento (Memento *memento) {state = Memento->getstate (); 
  } Memento *creatememento () {Return to New Memento (state); 
  } void SetState (string state) {this->state = state; 
  } void Showstate () {cout<< this->state <<endl; 
 
} 
}; int main () {originator *orIginator = new Originator ("November 11, 2012, Singles Day, one person, no girlfriend"); 
  Caretaker *caretaker = new Caretaker (); 
   
  Caretaker->setmemento (Originator->creatememento ()); 
 
  cout<< "November 11, 2012, the Bachelor's Day in the morning state is:" <<endl; 
 
  Originator->showstate (); 
  Originator->setstate ("at noon to attend the classmate's wedding, Jin fu Salt Help Hotel"); 
 
  Originator->showstate (); 
  cout<< "Back in the evening the state is" <<endl; 
  Originator->restorememento (Caretaker->getmemento ()); 
 
  Originator->showstate (); 
 
  cout<< "Like morning, HI" <<endl; 
  System ("pause"); 
return 0; 
 }

The output is like this

Memo Mode Applicability:

    • A (partial) state of an object must be saved at a particular point in time so that it can revert to its previous state when needed.
    • If an interface is used to allow other objects to get these states directly, it exposes the implementation details of the object and destroys the encapsulation of the object.
Related Article

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.