I recently read a book on design patterns, and I feel that there is no difficulty. It is nothing more than the application of polymorphism and inheritance. Let's talk about it today.
From the game perspective, the memo is a backup of the monster's attributes,
These attributes include blood volume, attack power, etc. Use menento to save these attributes. menento only have access methods.
Similar to a structure, the orginator class is used to access the menentor class (or structure), caretaker
Used to save the initial value of the menentor, so that the memorandum can be restored at any time.
In simple example, int I = 1; Int J; int m; the process of the memorandum is as follows: J = I; M = J;
Then J = 2; then J = m; this process is explained as follows: first save the value of I = 1 to J, and then save j = 1 to m;
In this way, the initial value is saved. Then, the value of J changes to 2, and then J is restored to 1 by saving the initial value m,
If you use a classCodeAs follows:
# Include "iostream. H"
Class memento // only includes attributes and simple fetch Function
{
Public:
Memento (){}
Memento (INT state) {This-> state = State ;}
Virtual ~ Memento (){}
Void getstate (Int & State) {state = This-> state ;}
Void setstate (INT state) {This-> state = State ;}
Public:
Int state;
};
Class Originator
{
Public:
Originator (){}
Virtual ~ Originator (){}
Memento * creatememento () {return new memento (State);} // Initialization
Void setmemento (memento me) {state = me. State;} // restore
Void show () {cout <state <Endl ;}
Public:
Int state;
};
Class caretaker
{
Public:
Caretaker (){}
Virtual ~ Caretaker (){}
Void getmenento (memento & Me) {me = This-> me ;}//
Void setmenento (memento * Me) {This-> me = * Me;} // Save the menento
Public:
Memento me;
};
Void main ()
{
Originator * o = new originator ();
O-> state = 1;
O-> show ();
Caretaker * c = new caretaker ();
C-> setmenento (o-> createmento ());
O-> state = 2;
O-> show ();
O-> setmemento (c-> me );
O-> show ();
}