备忘录模式:在不破坏封装行的前提下,捕获一个对象的内在状态,并在该对象之外保存这个状态,这样以后就可将该对象恢复到原来保存的状态。
发起备忘者类(Originator)
namespace MemoPattern.CLASS { class Originator { private string state; public string State { get { return state; } set { state = value; } } public Memento CreateMemento() { return new Memento(state); } public void ReadMemento(Memento memento) { this.state = memento.State; } public void Show() { Console.WriteLine("当前状态是:"+ state ); } } }
备忘录类(Memento)
namespace MemoPattern.CLASS { class Memento { private string state; public Memento(string state) { this.state = state; } public string State { get { return state; } } } }
备忘录管理类(Caretaker)
namespace MemoPattern.CLASS { class Caretaker { private Memento memento; public Memento Memento { get { return memento; } set { memento = value; } } } }
测试类(TestMain)
namespace MemoPattern { class TestMain { static void Main(string[] args) { Originator person = new Originator(); person.State = "开始游戏"; person.Show(); Caretaker manager = new Caretaker(); manager.Memento = person.CreateMemento(); person.State = "游戏失败"; person.Show(); person.ReadMemento(manager.Memento); person.Show(); Console.Read(); } } }
测试结果:
以下是对备忘录模式的具体应用:以保存游戏进度为例
保存游戏者(GameRole)
namespace MementoPatternTest.CLASS { class GameRole { private string attach; private string defense; private string life; /// <summary> /// 攻击力 /// </summary> public string Attach { get { return attach; } set { attach = value; } } /// <summary> /// 防御力 /// </summary> public string Defense { get { return defense; } set { defense = value; } } /// <summary> /// 生命力 /// </summary> public string Life { get { return life; } set { life = value; } } /// <summary> /// 保存游戏备忘 /// </summary> /// <returns></returns> public GameMemento SavaGameState() { return new GameMemento(life, attach, defense); } /// <summary> /// 获取保存的状态 /// </summary> /// <param name="gameState"></param> public void RecoverGameState(GameMemento gameState) { this.attach = gameState.Attach; this.life = gameState.Life; this.defense = gameState.Defense; } /// <summary> /// 游戏初始状态 /// </summary> public void InitGameState() { this.life = "100"; this.attach = "100"; this.Defense = "100"; } /// <summary> /// 游戏结束状态 /// </summary> public void GameOverState() { this.life = "0"; this.attach = "0"; this.Defense = "0"; } /// <summary> /// 状态显示 /// </summary> public void StateShow() { Console.WriteLine("角色当前状态:生命力:{0},攻击力{1},防御力{2}.",life,attach,defense); } } }
游戏备忘类(GameMemento)
namespace MementoPatternTest.CLASS { class GameMemento { private string life; private string attach; private string defense; /// <summary> /// 攻击力 /// </summary> public string Attach { get { return attach; } } /// <summary> /// 生命力 /// </summary> public string Life { get { return life; } } /// <summary> /// 防御力 /// </summary> public string Defense { get { return defense; } } public GameMemento(string life,string attach,string defense) { this.life = life; this.attach = attach; this.defense = defense; } } }
游戏备忘管理类(GameStateManage)
namespace MementoPatternTest.CLASS { class GameStateManage { private GameMemento gameMemento; public GameMemento GameMemento { get { return gameMemento; } set { gameMemento = value; } } } }
测试类(TestMain)
using MementoPatternTest.CLASS; namespace MementoPatternTest { class TestMain { static void Main(string[] args) { GameRole person = new GameRole(); person.InitGameState(); person.StateShow(); GameStateManage gameManage = new GameStateManage(); gameManage.GameMemento = person.SavaGameState(); person.GameOverState(); person.StateShow(); person.RecoverGameState(gameManage.GameMemento); person.StateShow(); Console.ReadLine(); } } }
测试结果: