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.