天天看點

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

介紹

在不破壞封裝性的前提下,捕獲一個對象的内部狀态,并在該對象之外儲存這個狀态。這樣以後就可将該對象恢複到儲存的狀态。

示例

有一個Message實體類,某個對象對它的操作有Insert()方法,隻有在插入時間符合要求的情況下才能插入成功,是以要求可以儲存和恢複Message對象的狀态,插入失敗後則恢複Message對象的狀态,然後隻更新時間,再次插入。

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

MessageModel

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using System;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using System.Collections.Generic;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using System.Text;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

namespace Pattern.Memento

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

{

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

    /**//// <summary>

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

    /// Message實體類(Memento)

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

    /// </summary>

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

    public class MessageModel

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /**//// <summary>

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /// 構造函數

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /// </summary>

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /// <param name="msg">Message内容</param>

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /// <param name="pt">Message釋出時間</param>

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        public MessageModel(string msg, DateTime pt)

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            this._message = msg;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            this._publishTime = pt;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        }

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        private string _message;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /// Message内容

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        public string Message

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            get 

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

{ return _message; }

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            set 

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

{ _message = value; }

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        private DateTime _publishTime;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /// Message釋出時間

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        public DateTime PublishTime

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

{ return _publishTime; }

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

{ _publishTime = value; }

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

    }

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

}

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

MessageModelCaretaker

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

    /// Memento管理者(Caretaker)

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

    public class MessageModelCaretaker

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        private MessageModel _messageModel;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /// Message實體對象(Memento)

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        public MessageModel MessageModel

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

{ return _messageModel; }

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

{ _messageModel = value; }

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

SqlMessage

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

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

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

    public class SqlMessage

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /// 插入Message

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /// <param name="mm">Message實體對象</param>

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /// <returns></returns>

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        public bool Insert(MessageModel mm)

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            // 秒數可以被5整除時,則執行插入操作

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

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

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

                // 代碼略

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

                return true;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            }

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            else

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

                return false;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /// 儲存Memento

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        public MessageModel SaveMemento()

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            return new MessageModel(_message, _publishTime);

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        /// 恢複Memento

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

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

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        public void RestoreMemento(MessageModel mm)

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            this._message = mm.Message;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            this._publishTime = mm.PublishTime;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

Test

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using System.Data;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using System.Configuration;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using System.Collections;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using System.Web;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using System.Web.Security;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using System.Web.UI;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using System.Web.UI.WebControls;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using System.Web.UI.WebControls.WebParts;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using System.Web.UI.HtmlControls;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

using Pattern.Memento;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

public partial class Memento : System.Web.UI.Page

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

    protected void Page_Load(object sender, EventArgs e)

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        SqlMessage m = new SqlMessage();

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        m.Message = "Message内容";

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        m.PublishTime = DateTime.Now;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        MessageModelCaretaker mmc = new MessageModelCaretaker();

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        mmc.MessageModel = m.SaveMemento();

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        bool bln = false;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

        while (!bln)

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            bln = m.Insert(new MessageModel(m.Message, m.PublishTime));

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            Response.Write(m.Message + " " + m.PublishTime.ToString() + " " + bln.ToString());

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            Response.Write("<br />");

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

            if (!bln)

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

                System.Threading.Thread.Sleep(1000);

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

                m.RestoreMemento(mmc.MessageModel);

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

                m.PublishTime = DateTime.Now;

樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)
樂在其中設計模式(C#) - 備忘錄模式(Memento Pattern)

運作結果

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://files.cnblogs.com/webabcd/Pattern.rar">[源碼下載下傳]</a>