原文:乐在其中设计模式(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)的更多相关文章

  1. 二十四种设计模式:备忘录模式(Memento Pattern)

    备忘录模式(Memento Pattern) 介绍在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到保存的状态. 示例有一个Message实体类,某 ...

  2. [设计模式] 18 备忘录模式Memento Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对备忘录模式是这样说的:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存 ...

  3. 备忘录模式-Memento Pattern(Java实现)

    备忘录模式-Memento Pattern Memento备忘录设计模式是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到以前保存的状态. 本文中的场景: 有一款游戏可以随时存档, ...

  4. 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)

    原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...

  5. 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)

    原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...

  6. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

  7. 乐在其中设计模式(C#) - 状态模式(State Pattern)

    原文:乐在其中设计模式(C#) - 状态模式(State Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 状态模式(State Pattern) 作者:webabcd 介绍 允 ...

  8. 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)

    原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...

  9. 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)

    原文:乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern) 作 ...

随机推荐

  1. RGB转为Lab空间

    虽然若干年前就看过了关于色彩空间的介绍,但是直到今天才自己动手写代码做这件事情.虽然网络上已经有很多现成的例子,但是一则仅仅适用于浮点型的数据,另一方面,在实现上也有一些尚可优化之处. 色彩模型除了最 ...

  2. Ubuntu9.04更新源

    1.sudo gedit /etc/apt/sources.list 编辑你的源列表,加入以下列表中你认为适合你的,拷贝到你的列表中,然后保存列表. Archive.ubuntu.com更新serve ...

  3. ueditor编辑文章时候,复制粘贴内容,原来的图片不能显示

    ueditor编辑文章时候.当现有文章有图片的时候, 再复制粘贴文本进去的时候.里面的图片就不能显示了, 编辑器查看文章Html代码,图片路径显示为:src="http://localhos ...

  4. jquery关于表格隐藏和显示问题

    1. 关于指定表格指定列隐藏显示 $(":checkbox[name*=month]").each(function(){ if(!$(this).attr("check ...

  5. hdu3555(数位dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题意:求区间[a,b]内包含有'49'的数的总个数. 分析:dp[pos][0]表示到第pos位 ...

  6. DataInputStream类readLong()引起的思考

    今天无意中看了下jdk中的DataInputStream类,然后看到readLong()方法,如下: private byte readBuffer[] = new byte[8]; public f ...

  7. HDU 1253-大逃亡(裸-DBFS)

    G - 胜利大逃亡 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  8. 学习NodeJS第一天:node.js介绍

    Node.JS 前辈 C 程序猿 Ryan Dahl(http://four.livejournal.com/)工程,根据 Google 著名的开源 JavaScript 发动机 V8 对于二次开发 ...

  9. kafka解释三的具体:发展Kafka应用

    一个.整体外观Kafka 我们知道.Kafka系统有三大组件:Producer.Consumer.broker . producers 生产(produce)消息(message)并推(push)送给 ...

  10. SignalR技术

    Asp.net SignalR快速入门 一.前言 之前半年时间感觉自己有点浮躁,导致停顿了半年多的时间没有更新博客,今天重新开始记录博文,希望自己可以找回初心,继续沉淀.由于最近做的项目中用到Sign ...