Memento mode Details-design mode (16)

Source: Internet
Author: User

Memento mode Source:

When designing a software system, we have the right to regret the user (which may actually be the user's right:), and we certainly need to provide actions such as undo for some critical operations. Then this regret medicine is provided by Memento mode.

Memento Mode Effect:

without compromising encapsulation, capture the internal state of an object and save the state outside that object so that it can be restored to its previously saved state.

Memento Mode the UML structure is shown in Figure 1:


Memento Pattern Composition:

Originator: Responsible for creating a memo memento that records its internal state at the current moment and can use the memo to restore its internal state. Originator can determine which internal states of the memento storage originator as needed.

Memento: is responsible for storing the internal state of the originator object and prevents objects other than originator from accessing the memo Memento. The memo has two interfaces, and caretaker can only see the narrow interface of the memo, which 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 saving the memo memento, can not operate or check the contents of the memo.

< Span style= "font-family: ' Microsoft Yahei '; line-height:35px ">memento Pattern code example:

Memento.h

#ifndef _memento_h_#define _memento_h_#include <string>using namespace std;//is responsible for storing the internal state of the originator object, You can also prevent objects other than originator from accessing the memo memento. The memo has two interfaces, and caretaker can only see the narrow interface of the memo, which 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.    Class Memento{private://Originator is a friend class that can access internal information, but other classes cannot access friend class originator;    Memento (const string& state);    ~memento ();    void SetState (const string& state);    String GetState (); String _state;};/    /responsible for creating a memo memento to record its internal state at the current moment, and to use the memo to restore the internal status class Originator{public:originator ();    Originator (const string& state);    ~originator ();    void Restoretomemento (memento* pmemento);    memento* Creatememento ();    void SetState (const string& state);    String GetState (); void Show ();p rotected:private:string _state;};/    /responsible for keeping memo memento, unable to operate or check the contents of the Memo Class Caretaker{public:caretaker ();    ~caretaker ();    void Setmemento (memento*); memento* Getmemento ();p rivate:memento* _memento;}; #endif </span>

Memento.cpp

#include "Memento.h" #include <iostream> #include <string>using namespace std; Memento::memento (const string& state) {this->_state = state;} Memento::~memento () {}string memento::getstate () {return this->_state;} void Memento::setstate (const string& state) {this->_state = state;} Originator::originator () {}originator::originator (const string& state) {this->_state = state;} Originator::~originator () {}string originator::getstate () {return this->_state;} void Originator::show () {cout << this->_state << Endl;} void Originator::setstate (const string& state) {this->_state = state;} memento* Originator::creatememento () {return new Memento (this->_state);} void Originator::restoretomemento (memento* pmemento) {this->_state = Pmemento->getstate ();} Caretaker::caretaker () {}caretaker::~caretaker () {}memento* Caretaker::getmemento () {return this->_memento;} void Caretaker::setmemento (memento* pmemento) {THIS-&GT;_memento = Pmemento;} </span>

Main.cpp

#include "Memento.h" int main () {    //Initialize object with state ' old '    originator* o = new Originator ("old");    O->show ();    Establish and save Memento    caretaker* Ptaker = new Caretaker ();    Ptaker->setmemento (O->creatememento ());    Change state    o->setstate ("New");    O->show ();    Recovery status    O->restoretomemento (Ptaker->getmemento ());    O->show ();    return 0;} </span>
applicability of the Memento model:

( 1). The memento mode comparison is useful for classes that are more complex in functionality, but need to maintain or record historical attributes, or if a property that needs to be saved is 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 Undo function of a command when using command mode in a system, Command mode can use Memo mode to store the state of the revocable operation.

Advantages and disadvantages of memento mode:

Advantages:

(1). When there is a change in the status of the initiator role, it can be a wrong change, and we use the memo mode to restore the error.

(2). The status of the backup is saved outside the initiator role, so that the initiator role does not need to manage the status of each backup.

Disadvantages:

(1). If the backed-up object has a large amount of information or is created and restored very frequently, it can cause significant performance overhead.
Summary of usage of memento mode: The key of Memento mode is to capture and save the internal state of a class without destroying the encapsulation line, so that the saved state can be used to implement the recovery operation. In order to achieve this goal, we can see in the following implementation that we have adopted a certain language support technology

The key to memento mode is friend class originator; we can see that Memento's interface is declared private, and originator is declared as a friend of Memento. We save the state of originator in the Memento class, and the memento interface is private, and it achieves the encapsulation effect.

In the originator class we provide a way to make the user regret: Restoretomemento (memento* mt); We can use this interface to make users regret it. In the sample program, we demonstrate this: the state of originator is changed from old to new and then back to old.

Memento mode Details-design mode (16)

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.