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