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