乐在其中设计模式(C#) - 桥接模式(Bridge Pattern)

[索引页]
[源码下载]


乐在其中设计模式(C#) - 桥接模式(Bridge Pattern)


作者: webabcd


介绍
将抽象部分与它的实现部分分离,使它们都可以独立地变化。


示例
有一个Message实体类,对它的操作有Insert()和Get()方法,现在使这些操作的抽象部分和实现部分分离。
MessageModel
using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Bridge
{
         /// <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; }
                }
        }
}
 
Message
using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Bridge
{
         /// <summary>
         /// 操作Message(Abstraction)
         /// </summary>
         public class Message
        {
                 private AbstractMessage _abstractMessage;
                 /// <summary>
                 /// 操作Message(Implementor)
                 /// </summary>
                 public AbstractMessage AbstractMessage
                {
                        get { return _abstractMessage; }
                        set { _abstractMessage = value; }
                }

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

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

namespace Pattern.Bridge
{
         /// <summary>
         /// 操作Message(RefinedAbstraction)
         /// </summary>
         public class MyMessage : Message
        {
                 /// <summary>
                 /// 获取Message
                 /// </summary>
                 /// <returns></returns>
                 public override List<MessageModel> Get()
                {
                        List<MessageModel> l = base.Get();

                         foreach (MessageModel mm in l)
                        {
                                mm.Message += "(RefinedAbstraction)";
                        }

                         return l;
                }
        }
}
 
AbstractMessage
using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Bridge
{
         /// <summary>
         /// 操作Message(Implementor)
         /// </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.Bridge
{
         /// <summary>
         /// Sql方式操作Message(ConcreteImplementor)
         /// </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.Bridge
{
         /// <summary>
         /// Xml方式操作Message(ConcreteImplementor)
         /// </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;
                }
        }
}
 
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.Bridge;

public partial class Bridge : System.Web.UI.Page
{
         protected void Page_Load( object sender, EventArgs e)
        {
                MyMessage m = new MyMessage();

                m.AbstractMessage = new SqlMessage();

                Response.Write(m.Insert( new MessageModel( "插入", DateTime.Now)));
                Response.Write( "<br />");
                Response.Write(m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString());
                Response.Write( "<br />");

                m.AbstractMessage = new XmlMessage();

                Response.Write(m.Insert( new MessageModel( "插入", DateTime.Now)));
                Response.Write( "<br />");
                Response.Write(m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString());
        }
}
 
 
运行结果
True
SQL方式获取Message(RefinedAbstraction) 2007-5-13 19:11:19
True
XML方式获取Message(RefinedAbstraction) 2007-5-13 19:11:19


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


OK
[源码下载]
 

你可能感兴趣的:(模式,Pattern,bridge,桥接,乐在其中)