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

    *备忘录模式(Memento):在不破坏封装性的前提下,捕获一个对戏的内部状态,
    *              并在该对象之外保存这个状态。这样以后就能恢复到原来保存的状态

    *Originator(发起人): 负责创建一个备忘录 Memento,用以记录当前时候他的内部状态,并可用于备忘录恢复。
    *Memento(备忘录):负责存储Originator对象的内部状态,并可防止Originator以外的其他对象访问备忘录Memento。
    *Caretaker(管理者):负责保存好备忘录。

    基础代码如下:

    Originator发起人类中需要提供 创建备忘录 跟 读取备忘录的方法。
    Caretaker 还要提供一个存储的第3方类。用于暂存 备忘录。(管理者)


                Originator o = new Originator();
                o.State = "On";
                o.Show();
    
                Caretaker c = new Caretaker();
                c.Memento = o.CreateMemento();
    
                o.State = "Off";
                o.Show();
    
                o.SetMemento(c.Memento);
                o.Show(); 
    
        /// <summary>
        /// 发起人
        /// </summary>
        public class Originator
        {
            public string State
            { get; set; }
            public Memento CreateMemento()
            {
                return (new Memento(State));
            } 
            public void SetMemento(Memento memento)
            {
                State = memento.State;
            } 
            public void Show()
            {
                Console.WriteLine("State=" + State);
            }  
        }
        /// <summary>
        /// Memento(备忘录)
        /// </summary>
        public class Memento
        {
            public string State
            { get; set; } 
            public Memento(string state)
            {
                this.State = state;
            }  
        }
        /// <summary>
        /// Caretaker(管理者)
        /// </summary>
        public class Caretaker
        {
            public Memento Memento
            { get; set; } 
        }    

     实例

                Console.WriteLine(" 数据初始化:");
                GameWuZhunShen WuZhenShen = new GameWuZhunShen();
                WuZhenShen.HP = "75120";
                WuZhenShen.MP = "175120";
                WuZhenShen.AP = "15120";
                WuZhenShen.SP = "3120";
                WuZhenShen.Show();
    
                Console.WriteLine(" 
    保存数据:(=========)");
                GMCaretaker GM = new GMCaretaker();
                GM.WuZhunShen = WuZhenShen.SaveState();
    
                Console.WriteLine(" 
    参战后数据:");
                WuZhenShen.HP = "5120";
                WuZhenShen.MP = "5720";
                WuZhenShen.AP = "15120";
                WuZhenShen.SP = "3120";
                WuZhenShen.Show();
    
    
                Console.WriteLine(" 
    恢复数据:");
                WuZhenShen.SetState(GM.WuZhunShen);
                WuZhenShen.Show();   
    
        /// <summary>
        /// 游戏角色 -- 武尊神
        /// </summary>
        public class GameWuZhunShen
        { 
            //属性值 
            public string HP
            { get; set; } 
            public string MP
            { get; set; } 
            public string AP
            { get; set; } 
            public string SP
            { get; set; } 
            //保存数据
            public WuZhunShenMemento SaveState()
            {
                return new WuZhunShenMemento(HP, MP, AP, SP);
            } 
            //读取保存的数据
            public void SetState(WuZhunShenMemento wuZhunShen)
            {
                HP = wuZhunShen.HP;
                MP = wuZhunShen.MP;
                AP = wuZhunShen.AP;
                SP = wuZhunShen.SP;
            } 
            public void Show()
            {
                Console.Write(" 武尊神:
    ");
                Console.Write(" HP:'{0}'
    ", HP);
                Console.Write(" MP:'{0}'
    ", MP);
                Console.Write(" AP:'{0}'
    ", AP);
                Console.Write(" SP:'{0}'
    ", SP);
            }
        }
    
        /// <summary>
        /// 游戏角色备忘录
        /// </summary>
        public class WuZhunShenMemento
        {
            //属性值 
            public string HP
            { get; set; } 
            public string MP
            { get; set; } 
            public string AP
            { get; set; } 
            public string SP
            { get; set; } 
            public WuZhunShenMemento(string _HP, string _MP, string _AP, string _SP)
            {
                HP = _HP;
                MP = _MP;
                AP = _AP;
                SP = _SP;
            } 
        }
    
        /// <summary>
        /// 系统管理者
        /// </summary>
        public class GMCaretaker
        {
            public WuZhunShenMemento WuZhunShen
            { get; set; }
        }
  • 相关阅读:
    poj 2226 Muddy Fields(最小点覆盖)
    hdu 5093 Battle ships(二分图最大匹配)
    poj 3020 Antenna Placement(二分图最大匹配)
    poj 3041 Asteroids(最小点覆盖)
    二分图的一些定理
    hdu 1083 Courses(二分图最大匹配)
    二分图最大匹配模板
    hdu 5094 Maze (BFS+状压)
    hdu 5092 Seam Carving (简单数塔DP,题没读懂,,不过可以分析样例)
    hdu 5090 Game with Pearls (额,, 想法题吧 / 二分图最大匹配也可做)
  • 原文地址:https://www.cnblogs.com/dragon-L/p/3786141.html
Copyright © 2011-2022 走看看