設計模式 之 備忘錄模式

來源:互聯網
上載者:User

重要使用場所是遊戲記錄的儲存,在打boss 掛了,可以從掛了前面一關開始而不用重新開始。

為什麼使用:以後項目的拓展和維護,而且比如遊戲角色的生命力,攻擊力,防禦力細節不能直接暴露在用戶端。

圖示:


代碼:

class Program    {        static void Main(string[] args)        {            //大戰boss之前            GameRole lixiaoyao = new GameRole();            lixiaoyao.GetIninState();            lixiaoyao.stateDisplay();            //儲存進度            RoleStateCaretaker stateAdmin = new RoleStateCaretaker();            stateAdmin.Memento = lixiaoyao.saveState();            //大戰boss時,損耗嚴重            lixiaoyao.Fight();            lixiaoyao.stateDisplay();            //恢複之前狀態            lixiaoyao.RecoveryState(stateAdmin.Memento);            lixiaoyao.stateDisplay();            Console.Read();        }    }    //遊戲角色類    class GameRole    {        private int vit;  //生命力        public int vitality        {            get            {                return vit;            }            set { vit = value; }        }        private int ack; //攻擊力        public int attack        {            get { return ack; }            set { ack = value; }        }        private int def; //防禦力        public int defense        {            get { return def; }            set { def = value; }        }        //狀態顯示        public void stateDisplay()        {            Console.WriteLine("當前角色狀態:");            Console.WriteLine("體力:{0} ", this.vit);            Console.WriteLine("攻擊力: {0}", this.ack);            Console.WriteLine("防禦力: {0}", this.def);            Console.WriteLine("");        }        //獲得初始狀態        public void GetIninState()        {            this.vit = 100;            this.ack = 100;            this.def = 100;        }        //戰鬥        public void Fight() {            this.vit = 0;            this.ack = 0;            this.def = 0;        }        //儲存角色狀態        public RoleStateMementto saveState() {            return (new RoleStateMementto(vit, ack, def));        }        //恢複角色狀態        public void RecoveryState(RoleStateMementto memento) {            this.vit = memento.vitality;            this.ack = memento.vitality;            this.def = memento.defense;        }    }    //角色狀態儲存箱      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;          }          //生命力          public int vitality {              get { return vit; }              set { vit = value; }          }          //攻擊力          public int attack{             get {return atk;}            set { atk = value; }          }          //防禦力          public int defense {              get { return def; }              set { def = value; }          }      }    //角色狀態管理者      class RoleStateCaretaker {          private RoleStateMementto memento;          public RoleStateMementto Memento {              get { return memento; }              set { memento = value; }          }      }

運行結果:



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.