码迷,mamicode.com
首页 > 其他好文 > 详细

备忘录(Memento)模式

时间:2014-06-14 22:17:03      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   color   get   

*备忘录模式(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(" \n保存数据:(=========)");
            GMCaretaker GM = new GMCaretaker();
            GM.WuZhunShen = WuZhenShen.SaveState();

            Console.WriteLine(" \n参战后数据:");
            WuZhenShen.HP = "5120";
            WuZhenShen.MP = "5720";
            WuZhenShen.AP = "15120";
            WuZhenShen.SP = "3120";
            WuZhenShen.Show();


            Console.WriteLine(" \n恢复数据:");
            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(" 武尊神:\n");
            Console.Write(" HP:‘{0}‘\n", HP);
            Console.Write(" MP:‘{0}‘\n", MP);
            Console.Write(" AP:‘{0}‘\n", AP);
            Console.Write(" SP:‘{0}‘\n", 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; }
    }

 

 

备忘录(Memento)模式,布布扣,bubuko.com

备忘录(Memento)模式

标签:style   class   blog   code   color   get   

原文地址:http://www.cnblogs.com/dragon-L/p/3786141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!