如果再回到从前 备忘录模式

18.1 如果再给我一次机会......

18.2 游戏存进度

namespace 备忘录模式
{
    class Program
    {
        static void Main(string[] args)
        {
            //大战Boss前,
            GameRole lixiaoyao = new GameRole();
            lixiaoyao.GetInitState();
            lixiaoyao.StateDisplay();

            //保存进度,
            GameRole backup = new GameRole();
            backup.Vitality = lixiaoyao.Vitality;
            backup.Attack = lixiaoyao.Attack;
            backup.Defense = lixiaoyao.Defense;

            //大战Boss时,损耗严重,
            lixiaoyao.Fight();
            lixiaoyao.StateDisplay();

            //恢复之前状态,
            lixiaoyao.Vitality = backup.Vitality;
            lixiaoyao.Attack = backup.Attack;
            lixiaoyao.Defense = backup.Defense;
            lixiaoyao.StateDisplay();

            Console.Read();
        }
    }

    //游戏角色类,
    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;
        }
    }

}
View Code

18.3 备忘录模式

在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可将该对象恢复到原先的保存状态,

18.4 备忘录模式基本代码

namespace 备忘录模式
{
    class Program
    {
        static void Main(string[] args)
        {
            Originator o = new Originator();

            o.State = "On";                      //Originator初始状态,状态属性为On,
            o.Show();

            Caretaker c = new Caretaker();

            c.Memento = o.CreateMemento();      //保存状态时,由于有了很好的封装,可以隐藏Originator的实现细节,

            o.State = "Off";                    //Originator改变了状态属性为Off,
            o.Show();

            o.SetMemento(c.Memento);            //恢复原初始状态,
            o.Show();

            Console.Read();
        }
    }

    //发起人类,
    class Originator
    {
        //需要保存的属性,可能有多个,
        private string state;

        public string State
        {
            get { return state; }
            set { state = value; }
        }

        //创建备忘录,将当前需要保存的信息导入并实例化一个Memento类,
        public Memento CreateMemento()
        {
            return (new Memento(state));
        }

        //恢复备忘录,将Memento导入并将相关数据恢复,
        public void SetMemento(Memento memento)
        {
            state = memento.State;
        }

        //显示数据,
        public void Show()
        {
            Console.WriteLine("State=" + state);
        }
    }

    //备忘录类,
    class Memento
    {
        private string state;

        //构造函数,将相关数据导入,
        public Memento(string state)
        {
            this.state = state;
        }

        //需要保存的属性,可以是多个,
        public string State
        {
            get { return state; }
        }
    }

    //管理者类,
    class Caretaker
    {
        private Memento memento;

        //得到或设置备忘录,
        public Memento Memento
        {
            get { return memento; }
            set { memento = value; }
        }
    }

}
View Code

就是把要保存的细节封装在了Memento中,哪一天要更改保存的细节也不用影响客户端了,

备忘录模式都用在什么场合?Memento模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性中的一小部分时,Originator可以根据保存的Memento信息还原到前一状态,

如果在某个系统中使用命令模式时,需要实现命令的撤销功能,那么命令模式可以使用备忘录模式来存储可撤销操作的状态,

有时一些对象的内部信息必须保存在对象以外的地方,但是必须要由对象自己读取,这时,使用备忘录可以把复杂的对象内部信息对其它的对象屏蔽起来,

最大的的作用还是在当角色的状态改变的时候,有可能这个状态无效,这时候就可以使用暂时存储起来的备忘录将状态复原,

18.5 游戏进度备忘

namespace 备忘录模式
{
    class Program
    {
        static void Main(string[] args)
        {

            //大战Boss前,
            GameRole lixiaoyao = new GameRole();
            lixiaoyao.GetInitState();
            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 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 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;
        }

        //获得初始状态,
        public void GetInitState()
        {
            this.vit = 100;
            this.atk = 100;
            this.def = 100;
        }

        //战斗,
        public void Fight()
        {
            this.vit = 0;
            this.atk = 0;
            this.def = 0;
        }
    }

    //角色状态存储箱,
    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 RoleStateMemento Memento
        {
            get { return memento; }
            set { memento = value; }
        }
    }

}
View Code
原文地址:https://www.cnblogs.com/huangxuQaQ/p/11301484.html