Design Pattern C ++ description ---- 17. Memento Pattern

Source: Internet
Author: User

I. Memorandum Mode

Definition: 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 previously saved state.

Structure:

 


Scope of use:

The Memento mode is applicable to classes with complex functions, but which need to maintain or record the property history. or, if the attributes to be saved are only a small part of many attributes, originator can be restored to the previous state based on the saved Memento information.

Code:

[Cpp] // memorandum class
// Stores the internal status of the Originator object
Class Memento
{
// Note: The methods in the memo class are private !!!!!!!
Private:
// This is the most critical part. Set Originator to the friend class.
// The Originator can access the internal information of the Memento class.
Friend class Originator;
 
Memento (){}
 
Memento (const string & sdt)
{
_ Sdt = sdt;
}
 
Void SetState (const string & sdt)
{
_ Sdt = sdt;
}

String GetState ()
{
Return _ sdt;
}
 
Private:
String _ sdt;
 
};
 
// Original Class
// Create a memorandum
Class Originator
{
Public:
Originator ()
{
_ Sdt = "";
_ Mt = 0;
}
 
Originator (const string & sdt)
{
_ Sdt = sdt;
_ Mt = 0;
}
 
// Create a memorandum to save the current information in a memorandum object
Memento * CreateMemento ()
{
Return new Memento (_ sdt );
}
 
// Restore the memorandum to restore the information on the object
Void RestoreToMemento (Memento * mt)
{
This-> _ sdt = mt-> GetState ();
}
 
String GetState ()
{
Return _ sdt;
}
 
Void SetState (const string & sdt)
{
_ Sdt = sdt;
}
 
Void PrintState ()
{
Cout <this-> _ sdt <"..." <endl;
}
 
Private:
String _ sdt;
Memento * _ mt;
};

 
// Test code
Int main (int argc, char * argv [])
{
Originator * o = new Originator ();
 
O-> SetState ("old"); // you can forget the previous status.
O-> PrintState ();
 
Memento * m = o-> CreateMemento (); // create a memorandum.
O-> SetState ("new"); // modify the status www.2cto.com
O-> PrintState ();
 
O-> RestoreToMemento (m); // restore the status before modification
O-> PrintState ();
 
Return 0;
}
// Memorandum
// Stores the internal status of the Originator object
Class Memento
{
// Note: The methods in the memo class are private !!!!!!!
Private:
// This is the most critical part. Set Originator to the friend class.
// The Originator can access the internal information of the Memento class.
Friend class Originator;

Memento (){}

Memento (const string & sdt)
{
_ Sdt = sdt;
}

Void SetState (const string & sdt)
{
_ Sdt = sdt;
}

String GetState ()
{
Return _ sdt;
}

Private:
String _ sdt;

};

// Original Class
// Create a memorandum
Class Originator
{
Public:
Originator ()
{
_ Sdt = "";
_ Mt = 0;
}

Originator (const string & sdt)
{
_ Sdt = sdt;
_ Mt = 0;
}

// Create a memorandum to save the current information in a memorandum object
Memento * CreateMemento ()
{
Return new Memento (_ sdt );
}

// Restore the memorandum to restore the information on the object
Void RestoreToMemento (Memento * mt)
{
This-> _ sdt = mt-> GetState ();
}

String GetState ()
{
Return _ sdt;
}

Void SetState (const string & sdt)
{
_ Sdt = sdt;
}

Void PrintState ()
{
Cout <this-> _ sdt <"..." <endl;
}

Private:
String _ sdt;
Memento * _ mt;
};

// Test code
Int main (int argc, char * argv [])
{
Originator * o = new Originator ();

O-> SetState ("old"); // you can forget the previous status.
O-> PrintState ();

Memento * m = o-> CreateMemento (); // create a memorandum.
O-> SetState ("new"); // modify the status
O-> PrintState ();

O-> RestoreToMemento (m); // restore the status before modification
O-> PrintState ();

Return 0;
}
 
Ii. Description

1. Memento is responsible for storing the internal status of the Originator object and preventing access memos from other objects (the specific implementation method is to make all its methods private ).


2. Memento declare Originator as its friend class, so that Originator can access all its functions, that is, it is open to Originator.

I think private and youyuan are the key to implementing the Memorandum model!

 


Author lwbeyond

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.