前进中的蜗牛

番茄大叔

水滴穿石,非一日之功;没有量变,何来质变。

备忘录模式(Memento Pattern)

模式定义

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

UML类图

  • 发起人(Originator)
  1. 创建一个含有当前的内部状态的备忘录对象。
  2. 使用备忘录对象恢复其内部状态。
  • 负责人(Caretaker)
  1. 负责保存备忘录对象。
  2. 从不操作或检查备忘录的内容。
  • 备忘录(Memento) 保存发起人Originator对象的内部状态

代码结构

	public static class MomentoApp
	{
		public static void Run()
		{
			Originator o = new Originator();
			o.State = "On";

			Caretaker c = new Caretaker();
			c.Memento = o.CreateMemento();

			o.State = "Off";

			o.SetMemento(c.Memento);
			Console.WriteLine(o.State);

		}
	}

	/// <summary>
	/// 发起人
	/// </summary>
	class Originator
	{
		public string State { get; set; }

		/// <summary>
		/// 创建备忘录
		/// </summary>
		/// <returns></returns>
		public Memento CreateMemento()
		{
			return (new Memento(this.State));
		}

		/// <summary>
		/// 安装备忘录回复
		/// </summary>
		/// <param name="memento"></param>
		public void SetMemento(Memento memento)
		{
			Console.WriteLine("Restoring state...");
			this.State = memento.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
		{
			set { _memento = value; }
			get { return _memento; }
		}
	}

情景案例

下面是一个游戏进度备忘录

	public static class MomentoApp
	{
		public static void Run()
		{
			//大战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();
		}
	}

	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 void GetInitState()
		{
			this.vit = 100;
			this.atk = 100;
			this.def = 100;
		}
		//战斗
		public void Fight()
		{
			this.vit = 0;
			this.atk = 0;
			this.def = 0;
		}
		//保存角色状态
		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;
		}
	}

	//角色状态存储箱
	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; }
		}
	}
posted @ 2018-02-22 18:12  LoveTomato  阅读(149)  评论(0编辑  收藏  举报