.NET设计模式(19):备忘录模式(Memento Pattern)
来源:互联网 发布:比邻软件怎么样 编辑:程序博客网 时间:2024/05/22 15:55
备忘录模式在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。[1]
备忘录模式(Memento Pattern)应用于保存和跟踪对象的状态,以便于必要的时候可以把对象恢复到以前的状态。备忘录模式可以为一个对象提供状态存储和状态恢复的手段。 一个备忘录是一个对象,这个对象保持了一些状态信息。
备忘录模可以在不暴露对象的内部结构的情况下完成把对象恢复到以前的状态功能。需要获取以前状态的对象是发起者(Originator)。当客户需要保存发起者的状态时,客户需要发起者的当前状态。发起者保存它所有的状态属性到一个独立的对象,这个对象就是备忘录(Memento),把备忘录(Memento)对象返回给客户, 通过负责人角色(Caretaker)将备忘录(Memento)对象保存起来。备忘录(Memento)对象可以看作在给定的时间点包含另一个对象内部状态的对象。备忘录(Memento)对象必须向除了发起者以外的所有对象隐藏发起者变量的值。当发起者允许备忘录(Memento)对象访问它的内部状态时,备忘录(Memento)对象应该被设计为对其他对象采取访问限制的对象。[2]
当客户需要把发起者的状态恢复到以前的状态时,它通过负责人角色(Caretaker)把备忘录(Memento)对象返回给发起者。发起者使用包含在备忘录(Memento)对象中的状态信息,恢复自己到备忘录(Memento)对象中保存的状态。[2]
在实际应用中, 我们希望能够执行一个取消操作,从而使系统状态回到先前的状态。比如, 通过单基击“Undo”按钮,确定何时获取并保存应用程序的状态信息,以及如果把应用程序恢复到保存前的状态。我们就可以使用备忘录模式.
using System.Collections.Generic;
using System.Text;
namespace MementoPattern
{
/// <summary>
/// 发起人角色:创建一个备忘录来保存自己的内部状态。
/// </summary>
public class Originator
{
private string _state;
/// <summary>
/// 内部状态
/// </summary>
public string State
{
get
{
return _state;
}
set
{
if (_state == value)
return;
_state = value;
}
}
/// <summary>
/// 创建备忘录对象
/// </summary>
/// <returns></returns>
public Memento CreateMemento()
{
Console.WriteLine("创建备忘录对象!");
return new Memento(_state);
}
/// <summary>
/// 恢复发起人的状态
/// </summary>
/// <param name="memento">状态</param>
public void RestoreMemento(Memento memento)
{
this._state = memento.State;
Console.WriteLine("恢复发起人的状态!");
}
}
/// <summary>
/// 备忘录角色:将发起人对象传入的内部状态保存起来,备忘录角色会根据发起人对象来判断已保存的内部状态,
/// 同时可以防止发起人(Originator)对象之外的任何对象读取与它相关的已保存的内部状态。
/// </summary>
public class Memento
{
private string _state;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="state"></param>
public Memento(string state)
{
this._state = state;
}
/// <summary>
/// 内部状态
/// </summary>
public string State
{
get
{
return _state;
}
set
{
if (_state == value)
return;
_state = value;
}
}
}
/// <summary>
/// 负责人角色:负责保存备忘录对象,但是不保存备忘录对象的内容。
/// </summary>
public class Caretaker
{
private Memento _saveMemento;
/// <summary>
/// 当前的备忘录对象
/// </summary>
public Memento SaveMemento
{
get
{
return _saveMemento;
}
set
{
if (_saveMemento == value)
return;
Console.WriteLine("将发起人对象的状态保存起来了!");
_saveMemento = value;
}
}
}
class Program
{
private static Originator o = new Originator();
private static Caretaker c = new Caretaker();
static void Main(string[] args)
{
//改变发起人对象的状态
o.State = "A";
//创建备忘录对象,并将发起人对象的状态保存起来
c.SaveMemento = o.CreateMemento();
//修改发起人的状态
o.State = "B";
//恢复发起人的状态
o.RestoreMemento(c.SaveMemento);
Console.ReadLine();
}
}
}
- .NET设计模式(19):备忘录模式(Memento Pattern)
- Net设计模式实例之备忘录模式(Memento Pattern)
- Memento Pattern(备忘录模式)
- 备忘录模式(Memento Pattern)
- 备忘录模式(Memento Pattern)
- 备忘录模式(Memento Pattern)
- 备忘录模式(Memento Pattern)
- 设计模式23:Memento Pattern (备忘录模式)
- 设计模式之备忘录模式(Memento Pattern)
- 设计模式------备忘录模式(Memento pattern)
- 设计模式之备忘录模式--- Pattern Memento
- Java设计模式--备忘录模式【Memento Pattern】
- 设计模式【备忘录模式Memento Pattern】
- 设计模式笔记22:备忘录模式(Memento Pattern)
- 设计模式(17):备忘录模式 Memento Pattern
- C#设计模式之备忘录模式(Memento Pattern)
- 设计模式——备忘录(Memento Pattern)模式
- Java设计模式——备忘录模式(Memento Pattern)
- spring和struts的集成
- 全角/半角转换
- Brain Fuck 和 WhiteSpace语言
- 如何修改SQL Server 2000身份验证模式和系统管理员Sa的登录密码
- .netfromwork 的理解
- .NET设计模式(19):备忘录模式(Memento Pattern)
- FLEX带进度指示的文件上传
- Test
- IIS配置php+soap
- SQL SERVER 2000用户sa 登录失败的解决办法
- 常用内存泄漏检测工具
- PHP调用SOAP
- Micro Framework WireProtocol协议介绍
- sql server2000 登陆失败?怎么回事?