Food growth note (9)-Memorandum mode and vegetable growth memorandum Mode

Source: Internet
Author: User

Food growth note (9)-Memorandum mode and vegetable growth memorandum Mode

A game role attack Applet:

Public class Program {public static void main (String [] args) {// GameRole lixiaoyao = new GameRole (); lixiaoyao. getInitState (); lixiaoyao. stateDisplay (); // Save the progress GameRole backup = new GameRole (); backup. setVit (lixiaoyao. getVit (); backup. setAtk (lixiaoyao. getAtk (); // vs Boss is, serious loss lixiaoyao. fight (); lixiaoyao. stateDisplay (); // restore the previous status lixiaoyao. setAtk (backup. getAtk (); lixiaoyao. setVit (backup. getVit (); lixiaoyao. stateDisplay () ;}} class GameRole {// alive private int vit; public int getVit () {return vit;} public void setVit (int vit) {this. vit = vit;} // attack force private int atk; public int getAtk () {return atk;} public void setAtk (int atk) {this. atk = atk;} // public void stateDisplay () {System. out. println ("current role status:"); System. out. println ("physical strength:" + getVit (); System. out. println ("Attack Power:" + getAtk ();} // obtain the initial state public void getInitState () {setVit (100); setAtk (100 );} // battle public void fight () {setVit (0); setAtk (0 );}}

After improvements using the memorandum model:

Public class Program {public static void main (String [] args) {// GameRole lixiaoyao = new GameRole (); lixiaoyao. getInitState (); lixiaoyao. stateDisplay (); // Save the progress RoleStateCaretaker stateAdmin = new RoleStateCaretaker (); stateAdmin. setMemento (lixiaoyao. saveState (); // vs Boss is, serious loss lixiaoyao. fight (); lixiaoyao. stateDisplay (); // restore the previous status lixiaoyao. recoveryState (stateAdmin. getMemento (); lixiaoyao. stateDisplay () ;}}// role status storage box class RoleStateMemento {private int vit; private int atk; public RoleStateMemento (int vit, int atk) {this. vit = vit; this. atk = atk;} // alive public int getVit () {return vit;} public void setVit (int vit) {this. vit = vit;} // attack force public int getAtk () {return atk;} public void setAtk (int atk) {this. atk = atk ;}}// role status manager class RoleStateCaretaker {private member memento; public Member getMemento () {return memento;} public void setMemento (rolestatemento memento) {this. memento = memento;} class GameRole {// alive private int vit; public int getVit () {return vit;} public void setVit (int vit) {this. vit = vit;} // attack force private int atk; public int getAtk () {return atk;} public void setAtk (int atk) {this. atk = atk;} // public void stateDisplay () {System. out. println ("current role status:"); System. out. println ("physical strength:" + getVit (); System. out. println ("Attack Power:" + getAtk ();} // obtain the initial state public void getInitState () {setVit (100); setAtk (100 );} // battle public void fight () {setVit (0); setAtk (0);} // Save the role state public RoleStateMemento saveState () {return (new RoleStateMemento (vit, atk);} // restore the role state public void recoveryState (rolestatemento memento) {setVit (memento. getVit (); setAtk (memento. getAtk ());}}
The memo mode is applicable to classes with complex functions, but which need to maintain or record the property history. or, if the attributes to be saved are only a small part of many attributes, originator can be restored to the previous state based on the saved Memento information.
 

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.