天天看點

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

介紹

動态地給一個對象添加一些額外的職責。就擴充功能而言,它比生成子類方式更為靈活。

示例

有一個Message實體類,某個對象對它的操作有Insert()和Get()方法,現在擴充這個對象的功能。

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

MessageModel

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using System;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using System.Collections.Generic;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using System.Text;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

namespace Pattern.Decorator

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

{

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    /**//// <summary>

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    /// Message實體類

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    /// </summary>

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    public class MessageModel

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        /**//// <summary>

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        /// 構造函數

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        /// </summary>

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

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

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

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

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        public MessageModel(string msg, DateTime pt)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            this._message = msg;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            this._publishTime = pt;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        }

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        private string _message;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        /// Message内容

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        public string Message

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            get 

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

{ return _message; }

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            set 

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

{ _message = value; }

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        private DateTime _publishTime;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        /// Message釋出時間

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        public DateTime PublishTime

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

{ return _publishTime; }

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

{ _publishTime = value; }

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    }

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

}

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

AbstractMessage

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    /// 操作Message的抽象構件(Component)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    public abstract class AbstractMessage

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        /// 擷取Message

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        /// <returns></returns>

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        public abstract List<MessageModel> Get();

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        /// 插入Message

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

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

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        public abstract bool Insert(MessageModel mm);

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

SqlMessage

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

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

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    public class SqlMessage : AbstractMessage

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        public override List<MessageModel> Get()

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            List<MessageModel> l = new List<MessageModel>();

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            l.Add(new MessageModel("SQL方式擷取Message", DateTime.Now));

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            return l;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        public override bool Insert(MessageModel mm)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            // 代碼略

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            return true;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

XmlMessage

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    /// Xml方式操作Message(ConcreteComponent)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    public class XmlMessage : AbstractMessage

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            l.Add(new MessageModel("XML方式擷取Message", DateTime.Now));

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

AbstractMessageWrapper

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    /// 裝飾AbstractMessage(Decorator)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    public abstract class AbstractMessageWrapper : AbstractMessage

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        private AbstractMessage _abstractMessage;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        /// <param name="abstractMessage">AbstractMessage</param>

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        public AbstractMessageWrapper(AbstractMessage abstractMessage)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            this._abstractMessage = abstractMessage;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            return _abstractMessage.Get();

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            return _abstractMessage.Insert(mm);

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

CheckUserWrapper

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    /// 擴充出使用者驗證的功能(ConcreteDecorator)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    public class CheckUserWrapper : AbstractMessageWrapper

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        public CheckUserWrapper(AbstractMessage abstractMessage)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            : base(abstractMessage)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            List<MessageModel> l = base.Get();

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            foreach (MessageModel mm in l)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

                mm.Message += "(經過使用者驗證)";

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            }

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            // 在這裡擴充功能

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

            return base.Insert(mm);

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

CheckInputWrapper

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    /// 擴充出輸入驗證的功能(ConcreteDecorator)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    public class CheckInputWrapper : AbstractMessageWrapper

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        public CheckInputWrapper(AbstractMessage abstractMessage)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

                mm.Message += "(經過輸入驗證)";

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

Test

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using System.Data;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using System.Configuration;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using System.Collections;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using System.Web;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using System.Web.Security;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using System.Web.UI;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using System.Web.UI.WebControls;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using System.Web.UI.WebControls.WebParts;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using System.Web.UI.HtmlControls;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

using Pattern.Decorator;

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

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

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

    protected void Page_Load(object sender, EventArgs e)

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        AbstractMessage message = new SqlMessage();

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        AbstractMessageWrapper amr = new CheckUserWrapper(message);

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        Response.Write(amr.Get()[0].Message + " " + amr.Get()[0].PublishTime.ToString());

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

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

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        AbstractMessageWrapper amr2 = new CheckInputWrapper(message);

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        Response.Write(amr2.Get()[0].Message + " " + amr2.Get()[0].PublishTime.ToString());

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        AbstractMessageWrapper amr3 = new CheckUserWrapper(message);

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        AbstractMessageWrapper amr4 = new CheckInputWrapper(amr3);

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

        Response.Write(amr4.Get()[0].Message + " " + amr4.Get()[0].PublishTime.ToString());

樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)
樂在其中設計模式(C#) - 裝飾模式(Decorator Pattern)

運作結果

SQL方式擷取Message(經過使用者驗證) 2007-5-13 19:34:01

SQL方式擷取Message(經過輸入驗證) 2007-5-13 19:34:01

SQL方式擷取Message(經過使用者驗證)(經過輸入驗證) 2007-5-13 19:34:01

參考

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

OK

<a href="http://files.cnblogs.com/webabcd/Pattern.rar">[源碼下載下傳]</a>