原文:乐在其中设计模式(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()[].PublishTime.ToString());

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



        m.AbstractMessage = new XmlMessage();



        Response.Write(m.Insert(new MessageModel("插入", DateTime.Now)));

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

        Response.Write(m.Get()[].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

[源码下载]

乐在其中设计模式(C#) - 桥接模式(Bridge Pattern)的更多相关文章

  1. 【设计模式】桥接模式 Bridge Pattern

    开篇还是引用吕振宇老师的那篇经典的文章<设计模式随笔-蜡笔与毛笔的故事>.这个真是太经典了,没有比这个例子能更好的阐明桥接模式了,这里我就直接盗来用了. 现在市面上卖的蜡笔很多,各种型号, ...

  2. python 设计模式之桥接模式 Bridge Pattern

    #写在前面 前面写了那么设计模式了,有没有觉得有些模式之间很类似,甚至感觉作用重叠了,模式并不是完全隔离和独立的,有的模式内部其实用到了其他模式的技术,但是又有自己的创新点,如果一味地认为每个模式都是 ...

  3. 二十四种设计模式:桥接模式(Bridge Pattern)

    桥接模式(Bridge Pattern) 介绍将抽象部分与它的实现部分分离,使它们都可以独立地变化. 示例有一个Message实体类,对它的操作有Insert()和Get()方法,现在使这些操作的抽象 ...

  4. 桥接模式(Bridge Pattern)

    1,定义           桥接模式(Bridge Pattern),也称为桥梁模式,其用意是将抽象化与实现化脱耦,使得两者可以独立的变化,它可以使软件系统沿着多个方向进行变化,而又不引入额外的复杂 ...

  5. 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)

    原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...

  6. 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)

    原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...

  7. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

  8. 乐在其中设计模式(C#) - 状态模式(State Pattern)

    原文:乐在其中设计模式(C#) - 状态模式(State Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 状态模式(State Pattern) 作者:webabcd 介绍 允 ...

  9. 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)

    原文:乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) 作者:webabc ...

随机推荐

  1. Delphi过程函数传递参数的八种方式

    今天一同事问我为什么有些过程函数里面有Var而有些没有,不解,遂到网上百度,得解.快哉,快哉. 在Delphi过程.函数中传递参数几个修饰符为Const.Var.Out.另一种不加修饰符的为默认按值传 ...

  2. C++ 多态性分析

    编译 - 时间多态性--函数重载 编译后的中间代码(例如GCC产生.o文件.此时还不是汇编语言)函数名字有变化,看以下两个样例. void cc_show(const char*str)     -& ...

  3. CF 519E(树上倍增求lca)

    传送门:A and B and Lecture Rooms 题意:给定一棵树,每次询问到达点u,v距离相等的点有多少个. 分析:按情况考虑: 1.abs(deep[u]-deep[v])%2==1时, ...

  4. iPhone、iPad强制关机

    情景:iPad測试应用过程中死机了. 解决:同一时候按住右上方的电源键和屏幕下方的HOME键大约10秒左右. 就会自己主动强制断电关机,然后重新启动. 强制重新启动后你会看到进入苹果的标志,然后进入主 ...

  5. 不可不知的DIP、IoC、DI以及IoC容器

    面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.当中.OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.DI以及Ioc容器等概念. 本文首先用实例阐述四个概 ...

  6. 2)JS动态生成HTML元素的爬取

    2)JS动态生成HTML元素的爬取 import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.W ...

  7. 用链表实现栈----《数据结构与算法分析----C语言描述》

    一.头文件: #ifndef _STACK_LINK_H_ #define _STACK_LINK_H_ struct stack_record; typedef struct stack_recor ...

  8. How to debug with IntelliJ IDEA + Grails 2.3.x (转)

    问题: 最近访问grails.org,看到grails framework已经发展到2.3.x了,不免想尝尝鲜.下载了最新的grails-2.3.x之后,创建了一个新的grails app. 添加Bo ...

  9. vmware: The file system upon which * resides is critically low on free space.

    The file system upon which ******.localized/Windows XP Professional.vmwarevm' resides is critically ...

  10. Service组件 总结 + 绑定理Service三种实现方式 Messager + Binder + AIDL

    在Android中进程按优先级可以分为五类,优先级从高到低排列: - 前台进程 该进程包含正在与用户进行交互的界面组件,比如一个Activity - 可视进程 该进程中的组件虽然没有和用户交互,但是仍 ...