Memorandum mode of Design Pattern

Source: Internet
Author: User

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 ();

}

 

 

 

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.