备忘录模式(Memento Pattern),在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以就该对象恢复到原先保存的状态。
当系统功能比较复杂,而且需要记录历史属性以便当需要时做恢复动作。Originator可以根据保存的Memento信息还原到前一状态。

Originator类:发起人。
CreateMemento方法,负责创建一个备忘录,用于记录当前时刻它的内部状态。
SetMemento方法,使用备忘录回复状态。
Show方法,展示状态。
Originator类:可以决定备忘录Memento存储Originator的哪些状态。
Memento类:备忘录,负责存储Originator的内部状态,并可防止Originator以外的其他对象访问备忘录Memento
Caretaker类:负责保存备忘录Memento,不能对备忘录的内容进行操纵和检查。
1、发起人类Originator
public class Originator
{
private string _state;
public string State
{
get { return _state; }
set { _state = value; }
}
/// <summary>
/// 创建备忘录,将当前要保存的信息导入并实例化备忘录
/// </summary>
public Memento CreateMemento()
return (new Memento(this.State));
/// 恢复备忘录,将Memento导入并将相关数据恢复
/// <param name="memento"></param>
public void SetMemento(Memento memento)
this.State = memento.State;
/// 展示状态数据
public void Show()
Console.WriteLine("当前状态是:"+this.State);
}
2、备忘录类Memento
public class Memento
public Memento(string state)
this.State = state;
3、管理者类Caretaker
public class Caretaker
private Memento _memento;
public Memento Memento
get { return _memento; }
set { _memento = value; }
4、客户端代码
static void Main(string[] args)
Originator o = new Originator();
//初始状态为On
o.State = "On";
o.Show();
//创建备忘录并保存状态
Caretaker caretaker = new Caretaker();
caretaker.Memento=o.CreateMemento();
//更改Originator状态=Off
o.State = "Off";
//恢复到原始状态
o.SetMemento(caretaker.Memento);
Console.ReadKey();
首先定义销售代理Noel van Halen的相关信息.然后保存到备忘录中,而定义销售代理Leo Welch相关信息。然后又回覆前一代理Noel van Halen的信息。。结构如下图所示
SalesProspect类:发起人
SaveMemento方法:创建备忘录
RestoreMemento方法:回覆备忘录
Memento类:备忘录,需要备份的信息有姓名、电话和预算
ProspectMemory类:负责保存备忘录Memento
1、发起人类SalesProspect
class SalesProspect
private string _name;
private string _phone;
private double _budget;
public string Name
{
get { return _name; }
set
{
_name = value;
Console.WriteLine("Name: " + _name);
}
public string Phone
get { return _phone; }
_phone = value;
Console.WriteLine("Phone: " + _phone);
public double Budget
get { return _budget; }
_budget = value;
Console.WriteLine("Budget: " + _budget);
public Memento SaveMemento()
Console.WriteLine("\nSaving state --\n");
return new Memento(_name, _phone, _budget);
public void RestoreMemento(Memento memento)
Console.WriteLine("\nRestoring state --\n");
this.Name = memento.Name;
this.Phone = memento.Phone;
this.Budget = memento.Budget;
class Memento
public Memento(string name, string phone, double budget)
this._name = name;
this._phone = phone;
this._budget = budget;
set { _name = value; }
set { _phone = value; }
set { _budget = value; }
3、管理者类ProspectMemory
class ProspectMemory
// Property
3、客户端代码
SalesProspect s = new SalesProspect();
s.Name = "Noel van Halen";
s.Phone = "(412) 256-0990";
s.Budget = 25000.0;
// Store internal state
ProspectMemory m = new ProspectMemory();
m.Memento = s.SaveMemento();
// Continue changing originator
s.Name = "Leo Welch";
s.Phone = "(310) 209-7111";
s.Budget = 1000000.0;
// Restore saved state
s.RestoreMemento(m.Memento);
本文对备忘录模式设计思想、结构和结构代码进行了分析,并以一实例进一步阐述了备忘录模式的C#实现。
版权
作者:灵动生活 郝宪玮
如果你认为此文章有用,请点击底端的【推荐】让其他人也了解此文章,
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。