Big talk design pattern C + + implementation-18th Chapter-Memo Mode

Source: Internet
Author: User

First, UML diagram



Second, the concept

Memo (Memento): captures the internal state of an object without compromising encapsulation, and saves the state outside that object. The object can then be restored to its previously saved state.


Third, the description

Role:

(1) Originator (INITIATOR): responsible for creating a memento that records its internal state at the current moment and can use the memo to restore the internal state. Originator can determine which internal states memento storage originator as needed.

(2) Memento (memo): responsible for storing the internal state of the originator object, and can prevent objects other than originator from accessing the memo Memento. The memo has two interfaces, caretaker can only see the narrow interface of the memo, and he can only pass the memo to other objects. Originator is able to see a wide interface that allows it to access all the data needed for the previous state.

(3) caretaker (manager): responsible for saving the package memo memento, can not operate or check the contents of the memo.

When do you use it?

The memento mode comparison is useful for classes that are more complex in functionality, but need to maintain or record property history, or if a property that needs to be saved is only a small part of many properties, originator can revert to the migration State based on the saved memento information.

Relationship to the command pattern?

If you need to implement the Undo function of a command when using command mode in a system, Command mode can use Memo mode to store the state of the revocable operation.

Iv. implementation of C + +

(1) Memento.h

#ifndef memento_h#define memento_h#include <iostream> #include <string>//memento class, Memo, Here is the role State storage box Rolestatememento classclass rolestatememento{private://Vitality int vit;//attack int atk;//defensive force int Def;public: Rolestatememento (int vit,int atk,int def) {this->vit=vit;this->atk=atk;this->def=def;} int getvitality () {return Vit;} void setvitality (int vit) {this->vit=vit;} int Getattack () {return ATK;} void Setattack (int atk) {THIS-&GT;ATK=ATK;} int Getdefense () {return def;} void Setdefense (int def) {this->def=def;}};/ /originator, initiator, here for the game role, Gamerole classclass gamerole{private://Vitality int vit;//attack int atk;//defensive Force int Def;public:int Getvitality () {return Vit;} void setvitality (int vit) {this->vit=vit;} int Getattack () {return ATK;} void Setattack (int atk) {THIS-&GT;ATK=ATK;} int Getdefense () {return def;} void Setdefense (int def) {this->def=def;} void Getinitstate () {this->vit=100;this->atk=100;this->def=100;} void Fight () {this->vit=0;this->atk=0;this->def=0;} void Statedisplay () {Std::cout<< "Current role Status:" <<std::endl;std::cout<< "Stamina:" <<this->vit<<std::endl;std::cout< < "vitality:" <<this->atk<<std::endl;std::cout<< "defensive Power:" <<this->def<<std::endl <<std::endl;} The Save Role State method returns the three state values of the game role by instantiating the role State bin rolestatememento* SaveState () {return new Rolestatememento (VIT,ATK,DEF);} The Restore role state method restores the status value in the external role state bin to the game role void Rocoverystate (Rolestatememento memento) {This->vit=memento. Getvitality (); This->atk=memento. Getattack (); This->def=memento. Getdefense ();}};/ /caretaker, manager, here for the game role management class, Rolestatecaretaker Classclass rolestatecaretaker{private:rolestatememento* Memento; public:rolestatememento* Getmemento () {return memento;} void Setmemento (rolestatememento* memento) {this->memento=memento;}}; #endif


(2) Client.h

#include "Memento.h" #include <iostream> #include <cstdlib>void main () {//Battle boss gamerole* Lixiaoyao=new Gamerole (); Lixiaoyao->getinitstate (); Lixiaoyao->statedisplay ();//Save Progress rolestatecaretaker* stateAdmin=new Rolestatecaretaker (); Stateadmin->setmemento (Lixiaoyao->savestate ());//battle boss, loss of serious lixiaoyao->fight (); Lixiaoyao->statedisplay ();//before resuming state               Lixiaoyao->rocoverystate (*stateadmin->getmemento ()); lixiaoyao- >statedisplay ();d elete lixiaoyao;delete stateadmin;system ("pause");}


(3) operation



Big talk design pattern C + + implementation-18th Chapter-Memo 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.