备忘录模式(Memento Pattern):
在不破环封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象回复到
保存前的状态。
/// <summary>
/// Originator:负责创建一个备忘录(Memento),用以记录当前时刻它的内部状态,并可以使用备忘录回复
/// 内部状态。Originator可根据需要决定Memento存储Originator的哪些内部状态
/// </summary>
class Originator
{
private string state;
//需要保存的属性,可能有多个
public string State
{
get { return state; }
set { state = value; }
}
//创建备忘录
public Memento CreateMemento()
{
return new Memento(state);
}
//回复备忘录
public void SetMemento(Memento memento)
{
state = memento.State;
}
public void Show()
{
Console.WriteLine("State="+state);
}
}
/// <summary>
/// 备忘录:负责存储Originator对象的内部状态,并可防止Originator以外的其他对象访问备忘录Memento。
/// 备忘录有两个接口,Caretaker只能看到备忘录的窄接口,它只能将备忘录传递给其他对象。
/// Originator能够看到一个宽接口,允许它访问返回到先前状态所需的所有数据
/// </summary>
class Memento
{
private string state;
//构造方法,讲相关数据导入
public Memento(string state)
{
this.state = state;
}
//需要保存的属性,可能有多个
public string State
{
get { return state; }
}
}
/// <summary>
/// 管理者:负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查。
/// </summary>
class Caretaker
{
private Memento memento;
//得到或设置备忘录
internal Memento Memento
{
get { return memento; }
set { memento = value; }
}
}
class Program
{
static void Main()
{
//初始状态为On
Originator o = new Originator();
o.State = "On";
o.Show();
//保存当前状态
//由于有了很好的封装性,可以隐藏Originator的实现细节
Caretaker c = new Caretaker();
c.Memento = o.CreateMemento();
o.State = "Off";
o.Show();
//回复初始状态
o.SetMemento(c.Memento);
o.Show();
}
}
优势和缺陷:
把保存的细节封装在了Memento中,那一天要更改保存的细节也不用影响客户端。
应用:
(1)对象状态的备忘足以使对象可以完全恢复到原来的状态。
(2)使用一个直接的接口来取得状态会使实现细节过程花,这样会打破对象的封装性。
实例:游戏进度备忘
/// <summary>
/// 游戏角色类
/// Originator类
/// </summary>
public 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}",vit);
Console.WriteLine("攻击力:{0}",atk);
Console.WriteLine("防御力:{0}", def);
Console.WriteLine();
}
//获取初始状态
public void GetInitState()
{
this.vit = 100;
this.atk = 100;
this.def = 100;
}
//战斗完时,状态全部为0
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;
}
}
public 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; }
}
}
public class RoleStateCaretaker
{
private RoleStateMemento memento;
public RoleStateMemento Memento
{
get { return memento; }
set { memento = value; }
}
}
客户端调用
//备忘录模式
GameRole gr = new GameRole();
gr.GetInitState();
gr.StateDisplay();
//保存进度
RoleStateCaretaker stateAdmin = new RoleStateCaretaker();
stateAdmin.Memento = gr.SaveState();
gr.Fight();
gr.StateDisplay();
//恢复
gr.RecoveryState(stateAdmin.Memento);
gr.StateDisplay();