The memo pattern is, by definition, a design pattern that has the function of forgetting to store its state information at a certain point outside of the object, and to recover the state of the object in its current situation by the state data saved in the memo whenever needed.
The memo pattern is designed to save the state of an object externally. Therefore, for the memo object there will be a maintainer Mementomanager object that maintains a list of all the memo objects corresponding to the object and, where appropriate, is responsible for erasing the memo objects. The class structure diagram for the pattern is referenced below:
The coding structure code for the pattern is referenced below:
1 namespaceMemento2 {3typedefintTdata;4 classMemento5 {6 Public:7 //some code here .....8FriendclassTarget;9 Private:TenInlineConstTdata Getstatedata ()Const{returnM_statedata;} One A Private: - tdata M_statedata; - the};//class Memento - - classTarget - { + Public: - //some code here ..... +memento* Creatememento () {return New(Std::nothrow) Memento ();} A voidSetmemento (memento*Pmemento) { at if(Nullptr! =Pmemento) { -Pmemento->m_statedata = This-M_statedata; - } - } - - Private: in tdata M_statedata; - to};//class Target + - classMementomanager the { * Public: $ //some code here .....Panax Notoginseng -~mementomanager () { This-Clearallmementos ();} the + voidClearallmementos () {/*some code here .....*/ } A the Private: +typedef std::d eque<memento*>Tmementodeq; - $ Tmementodeq M_deqmementos; $ -};//class Mementomanager - the}//namespace MementoMemento pattern Coding Structure Code reference
some digression of the memo pattern:
One of the features of object-oriented design is encapsulation, which is designed to effectively protect the State property of an object from being arbitrarily modified by the outside world. Therefore, it is best to ensure the (encapsulation) attribute for the previous target::m_statedata data, and it is best to only target the backup state data that is stored in its memento object. But for different languages, when using Memo mode, it is not always possible to ensure that the feature is not compromised. Languages like C + +, because of the support of the syntax mechanism of friend, it can be guaranteed on this feature. Other languages, such as Object Pascal, cannot be guaranteed. As a result, the final use of this pattern will also depend on the specific circumstances.
Application of Memo mode:
Memo mode can be used in many applications.
For example: Big to game data backup, such as: Diabol such as the classic large-scale single game, which has a role in the archive behavior, in fact, you can use the memo mode to design.
For example: the user operating habits in the project or the operation trace record, you can also use this mode. The memo mode can be permanently archived in conjunction with the local configuration.
For example: Memo mode with Command mode and iterator mode to implement functions like Photoshop's Operation history panel.
........
As long as the purpose of the model is understood, it is believed that many occasions in the actual project can be used.
"Behavioral" Memento mode