设计模式笔记(十三) —— 备忘录模式

备忘录模式(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个对象。这样以后就可将该对象恢复到原先保存的状态。
    使用场合:备忘录模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性的一小部分时,Originator可以根据保存的Memento信息还原到前一状态。 

  1. using System;
  2. /**
  3.  * 备忘录模式(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个对象。这样以后就可将该对象恢复到原先保存的状态。
  4.  * 使用场合:备忘录模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性的一小部分时,Originator可以根据保存的Memento信息还原到前一状态。
  5.  */
  6. namespace StuDesignMode.Memento
  7. {
  8.     #region 备忘录模式
  9.     //发起人
  10.     //class Originator
  11.     //{
  12.     //    public string State { get; set; }
  13.     //    public Memento CreateMemento()
  14.     //    {
  15.     //        return new Memento(this.State);            
  16.     //    }
  17.     //    public void Show()
  18.     //    {
  19.     //        Console.WriteLine("当前状态:{0}",this.State);
  20.     //    }
  21.     //    public void SetMemento(Memento m)
  22.     //    {
  23.     //        this.State = m.State;
  24.     //    }
  25.     //}
  26.     备忘录类
  27.     //class Memento
  28.     //{ 
  29.     //    private string _state;
  30.     //    public string State 
  31.     //    {
  32.     //        get { return this._state; }
  33.     //    }
  34.     //    public Memento(string state)
  35.     //    {
  36.     //        this._state = state;
  37.     //    }
  38.     //}
  39.     管理者类
  40.     //class Caretaker
  41.     //{
  42.     //    public Memento Memento { get; set; }    
  43.     //}
  44.     //class ClientTest
  45.     //{
  46.     //     static void Main(string[] args)
  47.     //     {
  48.     //        Originator o = new Originator();
  49.     //         o.State = "ON";
  50.     //         o.Show();
  51.     //         Caretaker c = new Caretaker();
  52.     //         c.Memento = o.CreateMemento();
  53.     //         o.State = "OFF";
  54.     //         o.Show();
  55.     //         o.SetMemento(c.Memento);
  56.     //         o.Show();
  57.     //     }
  58.     //}
  59.     #endregion
  60.     #region 示例代码
  61.     ///<summary>游戏角色</summary>
  62.     class GameRole
  63.     {
  64.         ///<summary>生命力</summary>
  65.         public int Vitaliy { getset; }
  66.         ///<summary>攻击力</summary>
  67.         public int Attack { getset; }
  68.         ///<summary>防御力</summary>
  69.         public int Defense { getset; }
  70.         ///<summary>存档</summary>
  71.         public RoleStateMemento SaveState()
  72.         {
  73.             return new RoleStateMemento(this.Vitaliy, this.Attack, this.Defense);
  74.         }
  75.         ///<summary>角色状态初始化</summary>
  76.         public void RoleInitState()
  77.         {
  78.             this.Vitaliy = 1000;
  79.             this.Attack = 500;
  80.             this.Defense = 200;
  81.         }
  82.         ///<summary>战斗</summary>
  83.         public void Fight()
  84.         {
  85.             this.Attack = 0;
  86.             this.Defense = 0;
  87.             this.Vitaliy = 0;
  88.         }
  89.         ///<summary>读档</summary>
  90.         public void RecoveryState(RoleStateMemento roleState)
  91.         {
  92.             this.Attack = roleState.Attack;
  93.             this.Defense = roleState.Defense;
  94.             this.Vitaliy = roleState.Vitaliy;
  95.         }
  96.         public void Show()
  97.         {
  98.             Console.WriteLine("======角色状态=========");
  99.             Console.WriteLine("生命力:{0}",this.Vitaliy);
  100.             Console.WriteLine("攻击力:{0}"this.Attack);
  101.             Console.WriteLine("防御力:{0}"this.Defense);
  102.             Console.WriteLine("====================/n");
  103.         }
  104.     }
  105.     ///<summary>角色状态存档</summary>
  106.     class RoleStateMemento
  107.     {
  108.         ///<summary>生命力</summary>
  109.         public int Vitaliy  { getset; }
  110.         ///<summary>攻击力</summary>
  111.         public int Attack { getset; }
  112.         ///<summary>防御力</summary>
  113.         public int Defense { getset; }
  114.         public RoleStateMemento(int vit, int ack, int def)
  115.         {
  116.             this.Vitaliy = vit;
  117.             this.Attack = ack;
  118.             this.Defense = def;
  119.         }
  120.     }
  121.     /// <summary>
  122.     /// 存档管理者
  123.     /// </summary>
  124.     class GameStateCaretaker
  125.     {
  126.         public RoleStateMemento RoleStateSaved { getset; }
  127.     }
  128.     class ClientTest
  129.     {
  130.         static void Main(string[] args)
  131.         {
  132.             //初始状态
  133.             GameRole lixiaoyao = new GameRole();
  134.             lixiaoyao.RoleInitState();
  135.             Console.WriteLine("李逍遥生死点时状态:");
  136.             lixiaoyao.Show();
  137.             //存档
  138.             GameStateCaretaker gsc = new GameStateCaretaker();
  139.             gsc.RoleStateSaved = lixiaoyao.SaveState();
  140.             Console.WriteLine("李逍遥大战Boss后状态:");
  141.             //大战Boss,死亡
  142.             lixiaoyao.Fight();
  143.             lixiaoyao.Show();
  144.             Console.WriteLine("李逍遥读档后状态:");
  145.             //读档重来
  146.             lixiaoyao.RecoveryState(gsc.RoleStateSaved);
  147.             lixiaoyao.Show();
  148.         }
  149.     }
  150.     #endregion
  151. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值