返回“設計模式(C#)系列文章索引”
介紹
在不破壞封裝性的前提下,捕獲一個對象的內部狀態,並在該對象之外保存這個狀態。這樣以後就可將該對象恢復到保存的狀態。
示例
有一個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; }
}
}
}
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; }
}
}
}
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;
}
}
}
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;
}
}
}
}
運行結果
Message內容 2007-5-23 21:32:13 False
Message內容 2007-5-23 21:32:14 False
Message內容 2007-5-23 21:32:15 True
參考
http://www.dofactory.com/Patterns/PatternMemento.aspx
OK