Memorandum mode of Design Pattern

Source: Internet
Author: User

An important place to use is the storage of game records. When playing a boss, you can start from the first stop without restarting.

Why: in the future, Project expansion and maintenance, such as the vitality, attack strength, and defensive strength of game roles cannot be directly exposed on the client.

Figure:


Code:

Class Program {static void Main (string [] args) {// GameRole lixiaoyao = new GameRole (); lixiaoyao. getIninState (); lixiaoyao. stateDisplay (); // Save the progress RoleStateCaretaker stateAdmin = new RoleStateCaretaker (); stateAdmin. memento = lixiaoyao. saveState (); // The loss of lixiaoyao. fight (); lixiaoyao. stateDisplay (); // restore the previous status lixiaoyao. recoveryState (stateAdmin. memento); lixiaoyao. stateDisplay (); Console. read () ;}// game role class GameRole {private int vit; // life public int vitality {get {return vit ;}set {vit = value ;}} private int ack; // attack force public int attack {get {return ack;} set {ack = value;} private int def; // defensive force public int defense {get {return def;} set {def = value ;}/// the status shows public void stateDisplay () {Console. writeLine ("current role status:"); Console. writeLine ("physical strength: {0}", this. vit); Console. writeLine ("Attack Strength: {0}", this. ack); Console. writeLine ("defensive force: {0}", this. def); Console. writeLine ("");} // obtain the initial state public void GetIninState () {this. vit = 100; this. ack= 100; this. def = 100;} // combat public void Fight () {this. vit = 0; this. ack = 0; this. def = 0;} // Save the role state public RoleStateMementto saveState () {return (new RoleStateMementto (vit, ack, def ));} // restore the role state public void RecoveryState (RoleStateMementto memento) {this. vit = memento. vitality; this. ack = memento. vitality; this. def = memento. defense ;}// role status storage box class RoleStateMementto {private int vit; private int atk; private int def; public RoleStateMementto (int vit, int atk, int def) {this. vit = vit; this. atk = atk; this. def = def ;}// vitality public int vitality {get {return vit ;}set {vit = value ;}// attack force public int attack {get {return atk ;} set {atk = value ;}/// defense Force public int defense {get {return def ;}set {def = value ;}}} // role status manager class RoleStateCaretaker {private RoleStateMementto memento; public RoleStateMementto Memento {get {return memento;} set {memento = value ;}}}

Running result:



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.