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