Big talk Design Pattern _ memorandum Pattern

Source: Internet
Author: User

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 ;}}

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.