[索引页]
[×××]


乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)


作者:webabcd


介绍
动态地给一个对象添加一些额外的职责。就扩展功能而言,它比生成子类方式更为灵活。


示例
有一个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)        /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// Message实体类
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        public class MessageModel
乐在其中设计模式(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)                        this._message = msg;
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        this._publishTime = pt;
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                private string _message;
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// Message内容
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public string Message
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        get { return _message; }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        set { _message = value; }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                private DateTime _publishTime;
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// Message发布时间
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public DateTime PublishTime
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        get { return _publishTime; }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        set { _publishTime = value; }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)}
 
AbstractMessage
乐在其中设计模式(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)        /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// 操作Message的抽象构件(Component)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        public abstract class AbstractMessage
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 获取Message
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <returns></returns>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public abstract List<MessageModel> Get();
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 插入Message
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <param name="mm">Message实体对象</param>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <returns></returns>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public abstract bool Insert(MessageModel mm);
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)}
 
SqlMessage
乐在其中设计模式(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)        /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// Sql方式操作Message(ConcreteComponent)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        public class SqlMessage : AbstractMessage
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 获取Message
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <returns></returns>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public override List<MessageModel> Get()
乐在其中设计模式(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)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 插入Message
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <param name="mm">Message实体对象</param>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <returns></returns>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public override bool Insert(MessageModel mm)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        // 代码略
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        return true;
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)}
 
XmlMessage
乐在其中设计模式(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)        /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// Xml方式操作Message(ConcreteComponent)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        public class XmlMessage : AbstractMessage
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 获取Message
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <returns></returns>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public override List<MessageModel> Get()
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        List<MessageModel> l = new List<MessageModel>();
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        l.Add(new MessageModel("XML方式获取Message", DateTime.Now));
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        return l;
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 插入Message
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <param name="mm">Message实体对象</param>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <returns></returns>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public override bool Insert(MessageModel mm)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        // 代码略
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        return true;
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)}
 
AbstractMessageWrapper
乐在其中设计模式(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)        /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// 装饰AbstractMessage(Decorator)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        public abstract class AbstractMessageWrapper : AbstractMessage
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                private AbstractMessage _abstractMessage;
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 构造函数
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <param name="abstractMessage">AbstractMessage</param>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public AbstractMessageWrapper(AbstractMessage abstractMessage)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        this._abstractMessage = abstractMessage;
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 获取Message
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <returns></returns>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public override List<MessageModel> Get()
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        return _abstractMessage.Get();
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 插入Message
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <param name="mm">Message实体对象</param>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <returns></returns>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public override bool Insert(MessageModel mm)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        return _abstractMessage.Insert(mm);
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)}
 
CheckUserWrapper
乐在其中设计模式(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)        /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// 扩展出用户验证的功能(ConcreteDecorator)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        public class CheckUserWrapper : AbstractMessageWrapper
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 构造函数
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <param name="abstractMessage">AbstractMessage</param>
乐在其中设计模式(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)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 获取Message
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <returns></returns>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public override List<MessageModel> Get()
乐在其中设计模式(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)                                mm.Message += "(经过用户验证)";
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        return l;
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 插入Message
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <param name="mm">Message实体对象</param>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <returns></returns>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public override bool Insert(MessageModel mm)
乐在其中设计模式(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)}
 
CheckInputWrapper
乐在其中设计模式(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)        /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// 扩展出输入验证的功能(ConcreteDecorator)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        public class CheckInputWrapper : AbstractMessageWrapper
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)        {
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 构造函数
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <param name="abstractMessage">AbstractMessage</param>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public CheckInputWrapper(AbstractMessage abstractMessage)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        : base(abstractMessage)
乐在其中设计模式(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)                /// <returns></returns>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public override List<MessageModel> Get()
乐在其中设计模式(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)                                mm.Message += "(经过输入验证)";
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                        return l;
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                }
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// 插入Message
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// </summary>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <param name="mm">Message实体对象</param>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                /// <returns></returns>
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)                public override bool Insert(MessageModel mm)
乐在其中设计模式(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)}
 
Test
乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)using System;
乐在其中设计模式(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)        protected void Page_Load(object sender, EventArgs e)
乐在其中设计模式(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)                Response.Write("<br />");
乐在其中设计模式(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)                Response.Write("<br />");
乐在其中设计模式(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


参考
http://www.dofactory.com/Patterns/PatternDecorator.aspx


OK
[×××]