天天看点

乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)

乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)

介绍

在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到保存的状态。

示例

有一个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; 

                /// Message内容 

                public string Message 

                        get { return _message; } 

                        set { _message = value; } 

                private DateTime _publishTime; 

                /// Message发布时间 

                public DateTime PublishTime 

                        get { return _publishTime; } 

                        set { _publishTime = value; } 

        } 

}

MessageModelCaretaker

        /// Memento管理者(Caretaker) 

        public class MessageModelCaretaker 

                private MessageModel _messageModel; 

                /// Message实体对象(Memento) 

                public MessageModel MessageModel 

                        get { return _messageModel; } 

                        set { _messageModel = value; } 

SqlMessage

        /// Sql方式操作Message(Originator) 

        public class SqlMessage 

                /// 插入Message 

                /// <param name="mm">Message实体对象</param> 

                /// <returns></returns> 

                public bool Insert(MessageModel mm) 

                        // 秒数可以被5整除时,则执行插入操作 

                        if (mm.PublishTime.Second % 5 == 0) 

                        { 

                                // 代码略 

                                return true; 

                        } 

                        else 

                                return false; 

                /// 保存Memento 

                public MessageModel SaveMemento() 

                        return new MessageModel(_message, _publishTime); 

                /// 恢复Memento 

                /// <param name="mm"></param> 

                public void RestoreMemento(MessageModel mm) 

                        this._message = mm.Message; 

                        this._publishTime = mm.PublishTime; 

Test

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

参考

<a href="http://www.dofactory.com/Patterns/PatternMemento.aspx" target="_blank">http://www.dofactory.com/Patterns/PatternMemento.aspx</a>

OK

<a href="http://down.51cto.com/data/100859" target="_blank">[源码下载]</a>

     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344578,如需转载请自行联系原作者