zoukankan      html  css  js  c++  java
  • 备忘录模式(Memento)

    重点概念

    1. 希望把存取状态的细节封装起来,而且最好是封装在外部的类当中,以体现职责分离。

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

    image

    3.要保存的细节给封装在了Memento中,修改备忘录中数据也不会影响客户端。

    4.备忘录模式适合功能复杂但需要维护或记录属性历史的类,或者可选的去备份和恢复某一个部分内容。

    5.命令模式可以使用备忘录模式实现撤销功能,在对象内部建立备忘录可以把备份信息屏蔽起来。

    6.当角色的状态改变的时候,有可能这个状态无效,这时候就可以使用暂时存起来的备忘录将状态恢复原来的状态。

    7.备忘录的核心含义是,在要备份的类中,声明一个备份成员对象,给自己建立一个备份方法,功能是将自己的成员都保存在这个备份对象中,再建立一个方法用于将备份对象中的内容恢复给自己。

    8.

    示例代码

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 备忘录模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                //初始要备份的类,状态设置了On
                Originator o = new Originator();
                o.State = "On";
                o.Show();
    
                Caretaker c = new Caretaker();
                //保存备忘录给管理者
                c.Memento = o.CreateMemento();
    
                //修改了状态为Off
                o.State = "Off";
                o.Show();
    
                //恢复回备份的状态为On
                o.SetMemento(c.Memento);
                o.Show();
    
                Console.Read();
    
            }
        }
    
        class Originator
        {
            //需要保存的属性,可能有多个
            private string state;
            public string State
            {
                get { return state; }
                set { state = value; }
            }
    
            /// <summary>
            /// 创建备忘录,将当前需要保存的信息导入并实例化一个Memeto对象
            /// </summary>
            /// <returns></returns>
            public Memento CreateMemento()
            {
                return (new Memento(state));
            }
            /// <summary>
            /// 恢复备忘录,将Memento 导入并将相关的数据恢复
            /// </summary>
            /// <param name="memento"></param>
            public void SetMemento(Memento memento)
            {
                state = memento.State;
            }
            /// <summary>
            /// 显示数据
            /// </summary>
            public void Show()
            {
                Console.WriteLine("State=" + state);
            }
        }
        /// <summary>
        /// 备忘录类,其中包含可以备份的对应成员
        /// </summary>
        class Memento
        {
            private string state;
    
            public Memento(string state)
            {
                this.state = state;
            }
    
            public string State
            {
                get { return state; }
            }
        }
        /// <summary>
        /// 管理者类
        /// </summary>
        class Caretaker
        {
            private Memento memento;
            //获取和存储备忘录
            public Memento Memento
            {
                get { return memento; }
                set { memento = value; }
            }
        }
    }
    游戏进度存储
    2009-12-06_104727 
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    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; }
            }
        }
    
    
    }
    冯瑞涛
  • 相关阅读:
    十天冲刺:第四天
    十天冲刺:第三天
    会议2.3
    会议2.2
    会议2.1
    团队绩效管理
    Alpha版(内部测试版)发布
    意见汇总
    建议汇总
    会议1.10
  • 原文地址:https://www.cnblogs.com/finehappy/p/1617919.html
Copyright © 2011-2022 走看看