[索引页]
[×××]


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


作者:webabcd


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


示例
有一个Message实体类,某个对象对它的操作有Insert()和Get()方法,现在扩展这个对象的功能。
 
MessageModel
using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Decorator
{
        /// <summary>
        /// Message实体类
        /// </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; }
                }
        }
}
 
AbstractMessage
using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Decorator
{
        /// <summary>
        /// 操作Message的抽象构件(Component)
        /// </summary>
        public abstract class AbstractMessage
        {
                /// <summary>
                /// 获取Message
                /// </summary>
                /// <returns></returns>
                public abstract List<MessageModel> Get();

                /// <summary>
                /// 插入Message
                /// </summary>
                /// <param name="mm">Message实体对象</param>
                /// <returns></returns>
                public abstract bool Insert(MessageModel mm);
        }
}
 
SqlMessage
using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Decorator
{
        /// <summary>
        /// Sql方式操作Message(ConcreteComponent)
        /// </summary>
        public class SqlMessage : AbstractMessage
        {
                /// <summary>
                /// 获取Message
                /// </summary>
                /// <returns></returns>
                public override List<MessageModel> Get()
                {
                        List<MessageModel> l = new List<MessageModel>();
                        l.Add(new MessageModel("SQL方式获取Message", DateTime.Now));

                        return l;
                }

                /// <summary>
                /// 插入Message
                /// </summary>
                /// <param name="mm">Message实体对象</param>
                /// <returns></returns>
                public override bool Insert(MessageModel mm)
                {
                        // 代码略
                        return true;
                }
        }
}
 
XmlMessage
using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Decorator
{
        /// <summary>
        /// Xml方式操作Message(ConcreteComponent)
        /// </summary>
        public class XmlMessage : AbstractMessage
        {
                /// <summary>
                /// 获取Message
                /// </summary>
                /// <returns></returns>
                public override List<MessageModel> Get()
                {
                        List<MessageModel> l = new List<MessageModel>();
                        l.Add(new MessageModel("XML方式获取Message", DateTime.Now));

                        return l;
                }

                /// <summary>
                /// 插入Message
                /// </summary>
                /// <param name="mm">Message实体对象</param>
                /// <returns></returns>
                public override bool Insert(MessageModel mm)
                {
                        // 代码略
                        return true;
                }
        }
}
 
AbstractMessageWrapper
using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Decorator
{
        /// <summary>
        /// 装饰AbstractMessage(Decorator)
        /// </summary>
        public abstract class AbstractMessageWrapper : AbstractMessage
        {
                private AbstractMessage _abstractMessage;

                /// <summary>
                /// 构造函数
                /// </summary>
                /// <param name="abstractMessage">AbstractMessage</param>
                public AbstractMessageWrapper(AbstractMessage abstractMessage)
                {
                        this._abstractMessage = abstractMessage;
                }

                /// <summary>
                /// 获取Message
                /// </summary>
                /// <returns></returns>
                public override List<MessageModel> Get()
                {
                        return _abstractMessage.Get();
                }

                /// <summary>
                /// 插入Message
                /// </summary>
                /// <param name="mm">Message实体对象</param>
                /// <returns></returns>
                public override bool Insert(MessageModel mm)
                {
                        return _abstractMessage.Insert(mm);
                }
        }
}
 
CheckUserWrapper
using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Decorator
{
        /// <summary>
        /// 扩展出用户验证的功能(ConcreteDecorator)
        /// </summary>
        public class CheckUserWrapper : AbstractMessageWrapper
        {
                /// <summary>
                /// 构造函数
                /// </summary>
                /// <param name="abstractMessage">AbstractMessage</param>
                public CheckUserWrapper(AbstractMessage abstractMessage)
                        : base(abstractMessage)
                {
                        
                }

                /// <summary>
                /// 获取Message
                /// </summary>
                /// <returns></returns>
                public override List<MessageModel> Get()
                {
                        List<MessageModel> l = base.Get();

                        foreach (MessageModel mm in l)
                        {
                                mm.Message += "(经过用户验证)";
                        }

                        return l;
                }

                /// <summary>
                /// 插入Message
                /// </summary>
                /// <param name="mm">Message实体对象</param>
                /// <returns></returns>
                public override bool Insert(MessageModel mm)
                {
                        // 在这里扩展功能

                        return base.Insert(mm);
                }

        }
}
 
CheckInputWrapper
using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Decorator
{
        /// <summary>
        /// 扩展出输入验证的功能(ConcreteDecorator)
        /// </summary>
        public class CheckInputWrapper : AbstractMessageWrapper
        {
                /// <summary>
                /// 构造函数
                /// </summary>
                /// <param name="abstractMessage">AbstractMessage</param>
                public CheckInputWrapper(AbstractMessage abstractMessage)
                        : base(abstractMessage)
                {
                        
                }

                /// <summary>
                /// 获取Message
                /// </summary>
                /// <returns></returns>
                public override List<MessageModel> Get()
                {
                        List<MessageModel> l = base.Get();

                        foreach (MessageModel mm in l)
                        {
                                mm.Message += "(经过输入验证)";
                        }

                        return l;
                }

                /// <summary>
                /// 插入Message
                /// </summary>
                /// <param name="mm">Message实体对象</param>
                /// <returns></returns>
                public override bool Insert(MessageModel mm)
                {
                        // 在这里扩展功能

                        return base.Insert(mm);
                }
        }
}
 
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.Decorator;

public partial class Decorator : System.Web.UI.Page
{
        protected void Page_Load(object sender, EventArgs e)
        {
                AbstractMessage message = new SqlMessage();

                AbstractMessageWrapper amr = new CheckUserWrapper(message);

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

                AbstractMessageWrapper amr2 = new CheckInputWrapper(message);

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

                AbstractMessageWrapper amr3 = new CheckUserWrapper(message);
                AbstractMessageWrapper amr4 = new CheckInputWrapper(amr3);

                Response.Write(amr4.Get()[0].Message + " " + amr4.Get()[0].PublishTime.ToString());
                Response.Write("<br />");
        }
}
 
 

运行结果
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
[×××]