Take playing a game as an example. Save the role status before fighting the boss, and restore the status before fighting the boss.
Package COM. WZS. design;/*** big talk design mode -- page186 memorandum mode ** @ author administrator **/public class memomode {public static void main (string [] ARGs) {// gamerole lixiaoyao = new gamerole (); lixiaoyao. getinitstate (); lixiaoyao. statedisplay (); // Save the progress rolestatecaretaker stateadmin = new rolestatecaretaker (); stateadmin. memento = lixiaoyao. savestate (); // vs boss, severe loss lixiaoyao. fight (); lixiaoyao. statedisplay (); // The status lixiaoyao. recoverystate (stateadmin. memento); lixiaoyao. statedisplay () ;}}/** game role */class gamerole {private int Vit; // vitality private int ATK; // attack force private int def; // defensive force // public void statedisplay () {system. out. println ("current role status:"); system. out. println ("vitality:" + this. vit); system. out. println ("Attack Power:" + this. ATK); system. out. println ("defensive force:" + this. def); system. out. println ();} // obtain the initial state public void getinitstate () {This. vit = 100; this. ATK = 100; this. def = 100;} // public void fight () {This. vit = 0; this. ATK = 0; this. def = 0;} // Save the role state public rolestatememento savestate () {return New rolestatememento (Vit, ATK, DEF);} // restore the role state public void recoverystate (rolestatememento memento) {This. vit = memento. getvit (); this. ATK = memento. getatk (); this. def = memento. getdef ();} public int getvit () {return Vit;} public void setvit (int vit) {This. vit = Vit;} public int getatk () {return ATK;} public void setatk (int atk) {This. ATK = ATK;} public int getdef () {return def;} public void setdef (int def) {This. def = def ;}/ ** role status storage box */class rolestatememento {private int Vit; private int ATK; private int def; Public rolestatememento (int vit, int ATK, int DEF) {This. vit = Vit; this. ATK = ATK; this. def = def;} public int getvit () {return Vit;} public void setvit (int vit) {This. vit = Vit;} public int getatk () {return ATK;} public void setatk (int atk) {This. ATK = ATK;} public int getdef () {return def;} public void setdef (int def) {This. def = def ;}/ ** role status manager */class rolestatecaretaker {public role memento; Public rolestatememento getmemento () {return memento;} public void setmemento (rolestatemento memento) {This. memento = memento ;}}