"Behavioral" Memento mode

Source: Internet
Author: User

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 Memento
Memento 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

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.