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