设计模式 - Memento Pattern(备忘录模式)
作者:webabcd
介绍
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到保存的状态。
示例
有一个Message实体类,某个对象对它的操作有Insert()方法,只有在插入时间符合要求的情况下才能插入成功,因此要求可以保存和恢复Message对象的状态,插入失败后则恢复Message对象的状态,然后只更新时间,再次插入。
MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Memento
{
/// <summary>
/// Message实体类(Memento)
/// </summary>
public class MessageModel
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public MessageModel(string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
}
private string _message;
/// <summary>
/// Message内容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
}
private DateTime _publishTime;
/// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Memento
{
/// <summary>
/// Message实体类(Memento)
/// </summary>
public class MessageModel
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public MessageModel(string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
}
private string _message;
/// <summary>
/// Message内容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
}
private DateTime _publishTime;
/// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
}
}
MessageModelCaretaker
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Memento
{
/// <summary>
/// Memento管理者(Caretaker)
/// </summary>
public class MessageModelCaretaker
{
private MessageModel _messageModel;
/// <summary>
/// Message实体对象(Memento)
/// </summary>
public MessageModel MessageModel
{
get { return _messageModel; }
set { _messageModel = value; }
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Memento
{
/// <summary>
/// Memento管理者(Caretaker)
/// </summary>
public class MessageModelCaretaker
{
private MessageModel _messageModel;
/// <summary>
/// Message实体对象(Memento)
/// </summary>
public MessageModel MessageModel
{
get { return _messageModel; }
set { _messageModel = value; }
}
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Memento
{
/// <summary>
/// Sql方式操作Message(Originator)
/// </summary>
public class SqlMessage
{
private string _message;
/// <summary>
/// Message内容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
}
private DateTime _publishTime;
/// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
// 秒数可以被5整除时,则执行插入操作
if (mm.PublishTime.Second % 5 == 0)
{
// 代码略
return true;
}
else
{
return false;
}
}
/// <summary>
/// 保存Memento
/// </summary>
/// <returns></returns>
public MessageModel SaveMemento()
{
return new MessageModel(_message, _publishTime);
}
/// <summary>
/// 恢复Memento
/// </summary>
/// <param name="mm"></param>
public void RestoreMemento(MessageModel mm)
{
this._message = mm.Message;
this._publishTime = mm.PublishTime;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Pattern.Memento
{
/// <summary>
/// Sql方式操作Message(Originator)
/// </summary>
public class SqlMessage
{
private string _message;
/// <summary>
/// Message内容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
}
private DateTime _publishTime;
/// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
// 秒数可以被5整除时,则执行插入操作
if (mm.PublishTime.Second % 5 == 0)
{
// 代码略
return true;
}
else
{
return false;
}
}
/// <summary>
/// 保存Memento
/// </summary>
/// <returns></returns>
public MessageModel SaveMemento()
{
return new MessageModel(_message, _publishTime);
}
/// <summary>
/// 恢复Memento
/// </summary>
/// <param name="mm"></param>
public void RestoreMemento(MessageModel mm)
{
this._message = mm.Message;
this._publishTime = mm.PublishTime;
}
}
}
Test
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pattern.Memento;
public partial class Memento : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlMessage m = new SqlMessage();
m.Message = "Message内容";
m.PublishTime = DateTime.Now;
MessageModelCaretaker mmc = new MessageModelCaretaker();
mmc.MessageModel = m.SaveMemento();
bool bln = false;
while (!bln)
{
bln = m.Insert(new MessageModel(m.Message, m.PublishTime));
Response.Write(m.Message + " " + m.PublishTime.ToString() + " " + bln.ToString());
Response.Write("<br />");
if (!bln)
{
System.Threading.Thread.Sleep(1000);
m.RestoreMemento(mmc.MessageModel);
m.PublishTime = DateTime.Now;
}
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pattern.Memento;
public partial class Memento : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlMessage m = new SqlMessage();
m.Message = "Message内容";
m.PublishTime = DateTime.Now;
MessageModelCaretaker mmc = new MessageModelCaretaker();
mmc.MessageModel = m.SaveMemento();
bool bln = false;
while (!bln)
{
bln = m.Insert(new MessageModel(m.Message, m.PublishTime));
Response.Write(m.Message + " " + m.PublishTime.ToString() + " " + bln.ToString());
Response.Write("<br />");
if (!bln)
{
System.Threading.Thread.Sleep(1000);
m.RestoreMemento(mmc.MessageModel);
m.PublishTime = DateTime.Now;
}
}
}
}
运行结果
Message内容 2007-5-23 21:32:13 False
Message内容 2007-5-23 21:32:14 False
Message内容 2007-5-23 21:32:15 True