[Design mode] memorandum mode (taking tomorrow's edge as an example)

Source: Internet
Author: User

 

1. Description

Memento: captures the Internal loading of an object without compromising encapsulation, and saves the State outside the object. In this way, the object can be restored to the previously saved state.

Three elements: [initiator] [memorandum] [Manager]

 

2. Demo

On the edge of tomorrow's film, General atango was thrown to the battlefield, and he slowly moved from the base
Woke up, confused to see the army walking around, dizzy to, then a Tang brother
I am still thrown to the battlefield, but I can wake up from the base every time I get down.
At the beginning of the game, it is equivalent to logging into the game and reading files automatically.

[Initiator] atango
[Memorandum] A Tang Ge reads files to the status
[Manager] Director

 

 

3. Upload the code

# Include <iostream> Using STD: cout; Using STD: Endl; /*************************************** **************** three elements of the design mode memorandum model: [initiator] [memorandum] [Manager] example: At the edge of tomorrow's film, General atango was thrown to the battlefield. atango slowly woke up from the base and found his army walking everywhere, my head was dizzy and confused. Later I was thrown to the battlefield, but I was able to wake up from the base every time I got down. This is equivalent to a memorandum in the game, automatic File Reading. [Initiator] A Tang Ge [memorandum] A Tang GE's reading status [Manager] Director *********************** * ********************************/static char * pstate = "Wake up slowly "; static char * pbehaivor = "Hold your head up slowly"; // memorandum class cmemento {public: cmemento (char * pstate, char * pbehaivor): m_pstate (pstate ), m_pbehaivor (pbehaivor) {} virtual ~ Cmemento () {} Char * getstate () const {return m_pstate;} Char * getbehaivor () const {return m_pbehaivor;} protected: char * m_pstate; char * m_pbehaivor ;}; // atango class ctom {public: ctom (char * pstate = 0, char * pbehaivor = 0, cmemento * pmemento = 0): m_pstate (pstate), m_pbehaivor (pbehaivor ), m_pmemento (pmemento) {} virtual ~ Ctom () {} cmemento * createmento () {return New cmemento (m_pstate, failed);} void setmemento (cmemento * pmemento) {m_pstate = pmemento-> getstate (); m_pbehaivor = pmemento-> getbehaivor ();} void show () const {cout <"\ nstate is" <m_pstate <", behaivor is "<m_pbehaivor <" \ n ";} void setenv (char * pstate, char * pbehaivor) {m_pstate = pstate; m_pbehaivor = pbehaivor;} protected: char * m _ Pstate; char * m_pbehaivor; cmemento * m_pmemento;}; // manager class ccaretaker {public: ccaretaker (): m_pmemento (0) {} virtual ~ Ccaretaker () {} cmemento * getmemento () const {return m_pmemento;} void setmemento (cmemento * pmemento) {principal = pmemento;} protected: cmemento * Principal ;}; int main () {// state: Wake up slowly. behaivor: Hold your head and sit down slowly. ctom Tom (pstate, pbehaivor); Tom. show (); cout <Endl; // manager, save the current status ccaretaker * pcaretaker = new ccaretaker (); pcaretaker-> setmemento (Tom. creatememento (); // Changes the current status of Tom. setenv Lost "); Tom. Show (); cout <" --------------- Note: I want to read the file! ------------- "<Endl; // restores the status Tom. setmemento (pcaretaker-> getmemento (); Tom. Show (); cout <Endl; return 0 ;}

 

 

 

4. Compile

G ++ main. cpp-O main. Out

 

5. Execute

./Main. Out

 

 

 

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.