乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)
原文:乐在其中设计模式(C#) - 备忘录模式(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; }
}
}
}
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整除时,则执行插入操作
)
{
// 代码略
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();
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
[源码下载]
乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)的更多相关文章
- 二十四种设计模式:备忘录模式(Memento Pattern)
备忘录模式(Memento Pattern) 介绍在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到保存的状态. 示例有一个Message实体类,某 ...
- [设计模式] 18 备忘录模式Memento Pattern
在GOF的<设计模式:可复用面向对象软件的基础>一书中对备忘录模式是这样说的:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存 ...
- 备忘录模式-Memento Pattern(Java实现)
备忘录模式-Memento Pattern Memento备忘录设计模式是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到以前保存的状态. 本文中的场景: 有一款游戏可以随时存档, ...
- 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)
原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...
- 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)
原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...
- 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)
原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...
- 乐在其中设计模式(C#) - 状态模式(State Pattern)
原文:乐在其中设计模式(C#) - 状态模式(State Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 状态模式(State Pattern) 作者:webabcd 介绍 允 ...
- 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)
原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...
- 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)
原文:乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern) 作 ...
随机推荐
- JMS学习(三)ActiveMQ Message Persistence(转)
1,JMS规范支持两种类型的消息传递:persistent and non-persistent.ActiveMQ在支持这两种类型的传递方式时,还支持消息的恢复.中间状态的消息(message are ...
- CF 519E(树上倍增求lca)
传送门:A and B and Lecture Rooms 题意:给定一棵树,每次询问到达点u,v距离相等的点有多少个. 分析:按情况考虑: 1.abs(deep[u]-deep[v])%2==1时, ...
- Android开发 更改返回button的图标
非常多的Android应用左上角都有返回button 在默认的情况下 ADT会默认给一个返回图标 而作为开发需求 非常多都要求定制一个新的图标 在Android的站点上 发现了2种能够更改的方法 1. ...
- vim配置(vimplus)
vim配置(vimplus) vimplus vimplus是vim的超级配置安装程序 github地址:https://github.com/chxuan/vimplus.git,欢迎star和fo ...
- Matlab实现PCA
在主成分分析(PCA)中,介绍了PCA的数学原理,其有用Matlab能够非常方便地对矩阵进行操作! 比方,用Matlab求多个样本的协方差矩阵.求矩阵的特征根和特征向量等. 以下介绍用Matlab实现 ...
- Java的图片处理工具类
import Java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- SWT中的GridLayout(转)例子不错
GridLayout 是一个非常强大的布局管理器,它可以实现很多复杂的布局,名字中暗示它将所有控件放置在类似网格的布局中.^__^GridLayout 有两个构造函数. GridLayout的构造函数 ...
- 部署 Redis 群集
Windows 部署 Redis 群集 1,下载Redis for windows 的最新版本,解压到 c:\Redis 目录下备用https://github.com/MSOpenTech/re ...
- 数据一致性(consistency)、服务可用性(availability)、分区容错性(partition-tolerance)
数据一致性(consistency).服务可用性(availability).分区容错性(partition-tolerance) 分布式系统理论基础 - CAP 2016-04-04 18:27 b ...
- Android 动态显示和隐藏软键盘
** * 动态设置软盘的显示和隐藏 * @author JPH */ public class MainActivity extends Activity implements OnClickList ...