备忘录模式(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。
以下给出备忘录模式的UML图:

Originator(发起人):负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。Originator可根据需要决定Memento存储Originator的哪些内部状态。
Memento(备忘录):负责存储Originnator对象的内部状态,并可防止Originator以外的其他对象访问备忘录Memento,备忘录有两个接口,Caretaker只能看到备忘录的窄接口,它只能将备忘录传递给其他对象。
Caretaker(管理者):负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查。
以下给出备忘录模式的基本代码结构:
namespace ConsoleApplication1
{
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)
{
state = memento.State; //恢复备忘录,将Memento导入并将相关数据恢复
}
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; }
}
}
//管理者(Caretaker)类
class Caretaker
{
private Memento memento;
public Memento Memento //得到或设置备忘录
{
get { return memento; }
set { memento = value; }
}
}
class Program
{
static void Main(string[] args)
{
Originator o = new Originator(); //Originator初始状态,状态属性为"On"
o.State = "On";
o.Show();
Caretaker c = new Caretaker(); //保存状态时,由于有了很好的封装,可以隐藏Originator
c.Memento = o.CreateMemento();
o.State = "Off"; //Originator改变了状态属性为"Off"
o.Show();
o.SetMemento(c.Memento); //恢复原初始状态
o.Show();
Console.ReadKey();
}
}
}
Memento模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性中的一小部分时,Originator可以根据保存的Memento信息还原到前一状态。如果在某个系统中使用命令模式时,需要实现命令的撤销功能,那么命令模式可以使用备忘录模式来存储可撤销操作的状态。
当角色的状态改变的时候,有可能这个状态无效,这时候就可以使用暂时存储起来的备忘录将状态复原。
回到《大话设计模式》中保存游戏进度的例子:
这是未使用备忘录模式的代码,注释中注明其缺点。
namespace ConsoleApplication1
{
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;
}
}
class Program
{
static void Main(string[] args)
{
//大战Boss前
GameRole lixiaoyao = new GameRole();
lixiaoyao.GetInitState(); //大战Boss前,获得初始角色状态
lixiaoyao.StateDisplay();
//保存进度
GameRole backup = new GameRole();
backup.Vitality = lixiaoyao.Vitality; //缺点:向客户端暴露了实现细节
backup.Attack = lixiaoyao.Attack;
backup.Defense = lixiaoyao.Defense;
//大战Boss时,拉柴了
lixiaoyao.Fight();
lixiaoyao.StateDisplay();
//恢复之前状态
lixiaoyao.Vitality = backup.Vitality;
lixiaoyao.Attack = backup.Attack;
lixiaoyao.Defense = backup.Defense;
lixiaoyao.StateDisplay();
Console.ReadKey();
}
}
}
下面来看看使用了备忘录模式的代码:
namespace ConsoleApplication1
{
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; }
}
}
class Program
{
static void Main(string[] args)
{
//大战Boss前
GameRole lixiaoyao = new GameRole();
lixiaoyao.GetInitState(); //游戏角色初始状态,三种指标数据均是100
lixiaoyao.StateDisplay();
//保存进度
RoleStateCaretaker stateAdmin = new RoleStateCaretaker(); //保存游戏进度,由于封装在Memento中,因此我们并不知道保存了哪些具体的角色数据
stateAdmin.Memento = lixiaoyao.SaveState();
//大战Boss时,拉柴了
lixiaoyao.Fight();
lixiaoyao.StateDisplay();
//恢复之前状态
lixiaoyao.RecoveryState(stateAdmin.Memento);
lixiaoyao.StateDisplay();
lixiaoyao.StateDisplay();
Console.ReadKey();
}
}
}
个人感觉,虽然在客户端隐藏了具体细节,但是好像那三个属性有了重复代码。
要注意备忘录模式的缺点,就是当角色状态需要完整存储到备忘录对象中,如果状态数据很大很多,那么在资源消耗上,备忘录对象会非常消耗内存。
来源:https://www.cnblogs.com/kissdodog/archive/2013/03/19/2969888.html