备忘录模式(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个对象。这样以后就可将该对象恢复到原先保存的状态。
使用场合:备忘录模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性的一小部分时,Originator可以根据保存的Memento信息还原到前一状态。
- using System;
- /**
- * 备忘录模式(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个对象。这样以后就可将该对象恢复到原先保存的状态。
- * 使用场合:备忘录模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性的一小部分时,Originator可以根据保存的Memento信息还原到前一状态。
- */
- namespace StuDesignMode.Memento
- {
- #region 备忘录模式
- //发起人
- //class Originator
- //{
- // public string State { get; set; }
- // public Memento CreateMemento()
- // {
- // return new Memento(this.State);
- // }
- // public void Show()
- // {
- // Console.WriteLine("当前状态:{0}",this.State);
- // }
- // public void SetMemento(Memento m)
- // {
- // this.State = m.State;
- // }
- //}
- 备忘录类
- //class Memento
- //{
- // private string _state;
- // public string State
- // {
- // get { return this._state; }
- // }
- // public Memento(string state)
- // {
- // this._state = state;
- // }
- //}
- 管理者类
- //class Caretaker
- //{
- // public Memento Memento { get; set; }
- //}
- //class ClientTest
- //{
- // static void Main(string[] args)
- // {
- // 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();
- // }
- //}
- #endregion
- #region 示例代码
- ///<summary>游戏角色</summary>
- class GameRole
- {
- ///<summary>生命力</summary>
- public int Vitaliy { get; set; }
- ///<summary>攻击力</summary>
- public int Attack { get; set; }
- ///<summary>防御力</summary>
- public int Defense { get; set; }
- ///<summary>存档</summary>
- public RoleStateMemento SaveState()
- {
- return new RoleStateMemento(this.Vitaliy, this.Attack, this.Defense);
- }
- ///<summary>角色状态初始化</summary>
- public void RoleInitState()
- {
- this.Vitaliy = 1000;
- this.Attack = 500;
- this.Defense = 200;
- }
- ///<summary>战斗</summary>
- public void Fight()
- {
- this.Attack = 0;
- this.Defense = 0;
- this.Vitaliy = 0;
- }
- ///<summary>读档</summary>
- public void RecoveryState(RoleStateMemento roleState)
- {
- this.Attack = roleState.Attack;
- this.Defense = roleState.Defense;
- this.Vitaliy = roleState.Vitaliy;
- }
- public void Show()
- {
- Console.WriteLine("======角色状态=========");
- Console.WriteLine("生命力:{0}",this.Vitaliy);
- Console.WriteLine("攻击力:{0}", this.Attack);
- Console.WriteLine("防御力:{0}", this.Defense);
- Console.WriteLine("====================/n");
- }
- }
- ///<summary>角色状态存档</summary>
- class RoleStateMemento
- {
- ///<summary>生命力</summary>
- public int Vitaliy { get; set; }
- ///<summary>攻击力</summary>
- public int Attack { get; set; }
- ///<summary>防御力</summary>
- public int Defense { get; set; }
- public RoleStateMemento(int vit, int ack, int def)
- {
- this.Vitaliy = vit;
- this.Attack = ack;
- this.Defense = def;
- }
- }
- /// <summary>
- /// 存档管理者
- /// </summary>
- class GameStateCaretaker
- {
- public RoleStateMemento RoleStateSaved { get; set; }
- }
- class ClientTest
- {
- static void Main(string[] args)
- {
- //初始状态
- GameRole lixiaoyao = new GameRole();
- lixiaoyao.RoleInitState();
- Console.WriteLine("李逍遥生死点时状态:");
- lixiaoyao.Show();
- //存档
- GameStateCaretaker gsc = new GameStateCaretaker();
- gsc.RoleStateSaved = lixiaoyao.SaveState();
- Console.WriteLine("李逍遥大战Boss后状态:");
- //大战Boss,死亡
- lixiaoyao.Fight();
- lixiaoyao.Show();
- Console.WriteLine("李逍遥读档后状态:");
- //读档重来
- lixiaoyao.RecoveryState(gsc.RoleStateSaved);
- lixiaoyao.Show();
- }
- }
- #endregion
- }