Originator(发起者):负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。Originator可根据需要决定Memento存储Originator的哪些内部状态
Memento(备忘录):负责存储Originator对象的内部状态,并可防止Originator以外的其他对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到备忘录 的窄接口,它只能将备忘录传递给其他对象。Originator能够看到一个宽接口,允许它访问返回到先前状态所需的所有数据。
Caretasker(管理者):负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查。
class Originator { private string state; public string State //需要保存的属性,可能有多个 { get { return state; } set { state = value; } } public Memento CreateMemento() //创建备忘录,将当前需要保存的信息导入并实例化出一个Memento对象 { return (new Memento(state)); } public void SetMemento(Memento memento) //恢复备忘录,将Memento导入并将相关数据恢复 { state = memento.State; } public void Show() //显示数据 { Console.WriteLine("State=" +state); } }
class Memento { private string state; public Memento(string state) //构造方法,将相关数据导入 { this.state = state; } public string State //需要保存的数据属性,可以是多个 { get { return state; } } }
class Caretasker { private Memento memento; public Memento Memento //得到或设置备忘录 { get { return memento; } set { memento = value; } } }
static void Main(string[] args) { Originator o = new Originator(); //Originator初始状态,状态属性为 “On” o.State = "On"; o.Show(); Caretasker c = new Caretasker(); //保存状态时,由于有了很好的封装,可以隐藏Originator的实现细节 c.Memento = o.CreateMemento(); o.State = "off"; //Originator改变了状态属性为“off” o.Show(); o.SetMemento(c.Memento); //恢复原初始状态 o.Show(); Console.Read(); }
我们在玩一些PC的单机游戏的时候,可能会因为打BOSS而去保存一下进度,以便打BOSS失败后再次恢复进度,再去挑战BOSS。这里其实就是一个备忘录模式。
static void Main(string[] args) { //大战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(); Console.Read(); }
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 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; } //获得初始状态 public void GetInitState() { this.vit = 100; this.atk = 100; this.def = 100; } //战斗 public void Fight() { this.vit = 0; this.atk = 0; this.def = 0; } }
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; } } }
1.提供了一种状态恢复的实现机制,使得到用户可以方便地回到一个特定的历史步骤
2.实现了对信息的封装,一个备忘录对象是一种原发器对象状态的表示,不会被其他代码所改动。
资源消耗过大,如果需要保存的原发器类的成员变量太多,就不可避免地需要占用大量的存储空间,每保存一次对象的状态都需要消耗一定的系统资源
适用环境
1、 需要保存一个对象在某一个时刻的状态或部分状态。
2、 如果用一个接口来让其他对象得到这些状态,将会暴露对象的实现细节并破坏对象的封装性,一个对象不希望外界直接访问其内部状态,通过负责人可以间接访问其内部状态。