您的位置:首页 > 其它

Net设计模式实例之备忘录模式(Memento Pattern)

2017-01-13 09:35 507 查看

一、备忘录模式简介(Brief Introduction)

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

二、解决的问题(What To Solve)

当系统功能比较复杂,而且需要记录历史属性以便当需要时做恢复动作。Originator可以根据保存的Memento信息还原到前一状态。

三、备忘录模式分析(Analysis)

1、备忘录模式结构



Originator类:发起人。

CreateMemento方法,负责创建一个备忘录,用于记录当前时刻它的内部状态。

SetMemento方法,使用备忘录回复状态。

Show方法,展示状态。

Originator类:可以决定备忘录Memento存储Originator的哪些状态。

Memento类:备忘录,负责存储Originator的内部状态,并可防止Originator以外的其他对象访问备忘录Memento

Caretaker类:负责保存备忘录Memento,不能对备忘录的内容进行操纵和检查。

2、备忘录模式代码

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));
}

/// <summary>
/// 恢复备忘录,将Memento导入并将相关数据恢复
/// </summary>
/// <param name="memento"></param>
public void SetMemento(Memento memento)
{
this.State = memento.State;
}

/// <summary>
/// 展示状态数据
/// </summary>
public void Show()
{
Console.WriteLine("当前状态是:"+this.State);
}
}
2、备忘录类Memento

public class Memento
{
private string _state;

public string State
{
get { return _state; }
set { _state = value; }
}

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.Show();

//恢复到原始状态
o.SetMemento(caretaker.Memento);
o.Show();

Console.ReadKey();
}

3、备忘录模式运行结果



四.实例分析(Example)

1、场景

首先定义销售代理Noel van Halen的相关信息.然后保存到备忘录中,而定义销售代理Leo Welch相关信息。然后又回覆前一代理Noel van Halen的信息。。结构如下图所示



SalesProspect类:发起人

SaveMemento方法:创建备忘录

RestoreMemento方法:回覆备忘录

Memento类:备忘录,需要备份的信息有姓名、电话和预算

ProspectMemory类:负责保存备忘录Memento

2、代码

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; }
set
{
_phone = value;
Console.WriteLine("Phone: " + _phone);
}
}
public double Budget
{
get { return _budget; }
set
{
_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;
}
}
2、备忘录类Memento

class Memento
{
private string _name;
private string _phone;
private double _budget;
public Memento(string name, string phone, double budget)
{
this._name = name;
this._phone = phone;
this._budget = budget;
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Phone
{
get { return _phone; }
set { _phone = value; }
}
public double Budget
{
get { return _budget; }
set { _budget = value; }
}
}
3、管理者类ProspectMemory

class ProspectMemory
{
private Memento _memento;
// Property
public Memento Memento
{
set { _memento = value; }
get { return _memento; }
}
}
3、客户端代码

static void Main(string[] args)
{
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);
Console.ReadKey();
}

3、实例运行结果



五、总结(Summary)

本文对备忘录模式设计思想、结构和结构代码进行了分析,并以一实例进一步阐述了备忘录模式的C#实现。

版权
作者:灵动生活 郝宪玮

出处:http://www.cnblogs.com/ywqu

如果你认为此文章有用,请点击底端的【推荐】让其他人也了解此文章,

加微信,与众多技术大咖交流: (请注明:cnblogs)


本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: