I understand the design pattern (C ++ implementation)-memento Pattern)

Source: Internet
Author: User
Overview:

When we play stand-alone games, we will always encounter various things of our elders. We will buy a bottle of vinegar for a while and a soy sauce for a while, which will delay our game process, but now we can "save the game". Our main base will not be torn down by our opponents when we make soy sauce.

The "save game" function is actually a good application of the memorandum mode. She captures the internal state of an object without disrupting the encapsulation, save the status outside the object. In this way, the object can be restored to the previously saved state. This is actually our redo, the Undo mode.

Class Diagram and instance: simple mode instance:
# Include <iostream >#include <string> using namespace STD; Class memento {PRIVATE: String State; public: memento () {state = "" ;}memento (string state) {This-> state = State;} string getstate () {return state;} void setstate (string state) {This-> state = State ;}}; class originator {PRIVATE: string State; public: originator () {state = "";} string getstate () {return state;} void setstate (string state) {This-> state = State ;} memento creatememento () {return memento (this-> state);} void restorememento (memento) {This-> setstate (memento. getstate () ;}}; class caretaker {PRIVATE: memento; public: memento getmemento () {return memento;} void setmemento (memento) {This-> memento = memento; }}; int main (INT argc, char * argv []) {originator; originator. setstate ("status 1"); cout <"initial status:" <originator. getstate () <Endl; caretaker. setmemento (originator. creatememento (); originator. setstate ("status 2"); cout <"status after change:" <originator. getstate () <Endl; originator. restoremento (caretaker. getmemento (); cout <"status after recovery:" <originator. getstate () <Endl ;}
Applicability:

This method is applicable to classes with complex functions but requires recording or maintaining the property history. If the attributes to be saved are only a small part of the attributes, originator can restore the saved memo to the previous state.

Advantages and disadvantages:

Advantages:

1) when the status of the initiator role changes, it may be an incorrect change. We can use the memo mode to restore the change.

2) The backup status is stored outside the initiator role, so that the initiator role does not need to manage the status of each backup.

Disadvantages:

1) if the backup object has a large amount of information or frequent creation or recovery operations, it may cause high performance overhead.

Lcl_data was originally created in csdn. Net [http://blog.csdn.net/lcl_data/article/details/9745019]

For other design patterns, see:Design patterns I understand

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.