Design Pattern memo memorandum Design Mode

Source: Internet
Author: User

This design mode is to simply record the current status and then use the recorded data to recover.

For example, we first have a class that needs to record the current state for relevant work:

class Memo;class Human{public:string state;Memo *makeMemo();void restroDataFromMemo(Memo *m);void show(){cout<<"State: "<<state<<endl;}};

The state can be any defined data, depending on the actual situation.

Then we design a class that can keep state data according to this state:

class Memo{public:string state;Memo(string s) : state(s){}};

The following describes how to maintain and restore data:

Memo *Human::makeMemo(){return new Memo(state);}void Human::restroDataFromMemo(Memo *m){state = m->state;}

Then, let's look at the main function. We can use these two methods to maintain and recover data to convert the state and then restore it to the previous state:

int main(){Human human;human.state = "Get Ready";cout<<"\nThe current state:\n";human.show();Memo *m = human.makeMemo();cout<<"\nThe memo saved state:\n"<<m->state<<endl;cout<<"\nHuman set to new state:\n";human.state = "Handle distraction";human.show();cout<<"\nNow we use memo to restor previouse info.\n";human.restroDataFromMemo(m);human.show();delete m;return 0;}



In general, this is a very simple design model.

I think this design pattern can use general non-class methods to record the status, but when the data volume is very large, memo class can simplify the code, in addition, you can use classes to remember the data more easily, because the data is connected to a class.

If you want to make the design mode more complex, it is the State problem. For example, when the State is very complex, the design mode will naturally become complicated.

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.