备忘录模式

来源:互联网 发布:美工的进阶之路 编辑:程序博客网 时间:2024/06/03 19:09


有一个状态存储箱,还有一个管理者,类X保存状态为一个状态存储箱对象交由管理者保管,管理者可以导入和导出状态管理箱,类X可以接受一个状态存储箱(状态存储箱作为参数)来恢复状态。




using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 备忘录模式{    class GameRole    {        //生命力        private int vit;        public int Vitality        {            get { return vit; }            set { vit = value; }        }        //攻击力        private int atk;        public int Attack        {            get { return atk; }            set { atk = 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.atk);            Console.WriteLine("防御力:{0}", this.def);            Console.WriteLine("");        }        //获得初始状态        public void GetInitState()        {            this.vit = 100;            this.atk = 100;            this.def = 100;        }        //战斗        public void Fight()        {            this.vit = 0;            this.atk = 0;            this.def = 0;        }        public RoleStateMemento SaveState()        {            return (new RoleStateMemento(vit, atk, def));        }        //恢复角色状态        public void RecoveryState(RoleStateMemento memento)        {            this.vit = memento.Vitality;            this.atk = memento.Attack;            this.def = memento.Defense;        }    }    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 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 RoleStateMemento memento;        public void ImportState(RoleStateMemento Record)        {            memento = Record;        }        public RoleStateMemento ExportState()        {            return memento;        }    }    class Program    {        static void Main(string[] args)        {            //大战Boss前            GameRole lixiaoyao = new GameRole();            lixiaoyao.GetInitState();            lixiaoyao.StateDisplay();            //保存进度            RoleStateCaretaker admin = new RoleStateCaretaker();            admin.ImportState(lixiaoyao.SaveState());            lixiaoyao.Fight();            lixiaoyao.StateDisplay();            lixiaoyao.RecoveryState(admin.ExportState());            lixiaoyao.StateDisplay();        }    }}




角色当前状态:体力:100攻击力:100防御力:100角色当前状态:体力:0攻击力:0防御力:0角色当前状态:体力:100攻击力:100防御力:100请按任意键继续. . .

原创粉丝点击