原文:乐在其中设计模式(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. c语言数组应用--统计随机数并打印直方图

    C标准库中生成伪随机数的是rand函数,使用这个函数需要包含头文件stdlib.h,它没有参数,返回值是一个介于0和RAND_MAX之间的接近均匀分布的整数.RAND_MAX是该头文件中定义的一个常量 ...

  2. “>>”和“>>>” java

    “>>”算术右移运算符, 表示带符号右移,它使用最高位填充移位后左侧的空位.右移的结果为:每移一位,第一个操作数被2除一次,移动的次数由第二个操作数确定.按二进制形式把所有的数字向右移动对 ...

  3. 敏捷开发-Scrum 真实

    近期研究前 Scrum 数据编译的文件,在接下来的团队和项目开发.项目根据该引入 Scrum 一些练习,提高团队成员和项目之间的交付质量的合作. 参考资料: <轻松Scrum之旅-敏捷开发故事& ...

  4. jQuery 弹出窗口的形式一直是具体案件的中心

    在网上查 多 不是不符合无效;因此,一些自己总结,解决这个问题   原则: 常见问题: 弹出层居中了,背景也是半透明的  可是发现一拉动滚动栏立即就露馅了发现背景仅仅设置了屏幕所在段,其它部分都是原来 ...

  5. mongodb中分页显示数据集的学习

    这次继续看mongodb中的分页.首先依然是插入数据: 1) db.Blog.insert( { name : "Denis",  age : 20, city : "P ...

  6. ARM裸编程系列---UART

    S5PV210 UART说明 通用异步收发器缩写UART,这是UNIVERSAL ASYNCHRONOUS RECEIVER AND TRANSMITTER.它被用来传送串行数据.当发送数据,CPU将 ...

  7. HighChart学习-更新数据data Series与重绘

    一:HighChart介绍 基于JQuery的纯JavaScript的图标库,支持各种图表显示,同时还支持Mootools 与Prototype详细版本支持在这里: JQuery 1.3.2 - 1. ...

  8. hdu3974(线段树+dfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定点的上下级关系,规定如果给i分配任务a,那么他的所有下属.都停下手上的工作,开始做a. ...

  9. 程序猿进化 - 在拉钩子1024对APE节讲座计划

    注意:下面这篇文章来自于我在网上拉勾1024对APE节现场演示程序. 我是蒋宇捷,信天创投的合伙人.之前是百度魔图的联合创始人. 我先做个自我介绍,事实上每次介绍自己事实上是非常痛苦的事情,由于我前不 ...

  10. Storm具体解释一、Storm 概述

    一.Storm概述      Storm是一个分布式的.可靠的.零失误的流式数据处理系统. 它的工作就是委派各种组件分别独立的处理一些简单任务.在Storm集群中处理输入流的是Spout组件,而Spo ...