C#设计模式系列:桥接模式(Bridge)
1、桥接模式简介
1.1>、定义
当一个抽象可能有多个实现时,通常用继承来进行协调。抽象类定义对该抽象的接口,而具体的子类则用不同的方式加以实现。继承机制将抽象部分与它的实现部分固定在一起,使得难以对抽象部分和实现部分独立地进行修改、扩充和重用。
如果一个抽象类或接口有多个具体实现子类,而这些子类之中有内容或概念上重叠,需要我们把抽象的共同部分各自独立开来:即原来是准备放在一个接口里,现在需要设计两个接口——抽象接口和行为接口。然后再分别针对各自的具体子类定义抽象接口和行为接口的方法和调用关系。
桥接模式的用意是将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化。
抽象化(Abstraction)
存在于多个实体中的共同的概念性联系,即为抽象化。作为一个过程,抽象化就是忽略一些信息,从而把不同的实体当做同样的实体对待。
实现化(Implementation)
抽象化给出的具体实现,即为实现化。
脱耦
耦合是指两个对象的行为的某种强关联,脱耦则是要去掉它们之间的强关联。在这里,脱耦是指将抽象化和实现化之间的耦合解脱,或者将它们之间的强关联改换成弱关联。将两者之间的继承关系改为聚合关系,就是将它们之间的强关联改换成为弱关联。
桥接模式中的脱耦,是指抽象化和实现化之间使用组合/聚合关系,而不是继承关系,从而使两者可以相对独立地变化。
1.2>、使用频率
中等
2、桥接模式结构图

桥接模式的结构包括Abstraction、RefinedAbstraction、Implementor、ConcreteImplementorA和ConcreteImplementorB五个部分,其中:
◊ Abstraction:定义抽象类的接口,它维护了一个指向Implementor类型对象的指针。
◊ RefinedAbstraction:扩充由Abstraction定义的接口;
◊ Implementor:定义实现类的接口,该接口不一定要与Abstraction的接口完全一致,事实上两个接口可以完全不同。一般情况,Implementor接口仅为提供基本操作,而Abstraction则定义了基于基本操作的较高层次操作。
◊ ConcreteImplementorA和ConcreteImplementorB:实现Implementor接口并定义它的具体实现。
在桥接模式中,两个类Abstraction和Implementor分别定义了抽象与行为类型的接口,通过调用两接口的子类实现抽象与行为的动态组合。
3、桥接模式结构实现

Implementor.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Structural
{
public abstract class Implementor
{
public abstract void Operation();
}
}
Abstraction.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Structural
{
public class Abstraction
{
protected Implementor implementor; public Implementor Implementor
{
set
{
implementor = value;
}
} public virtual void Operation()
{
implementor.Operation();
}
}
}
ConcreteImplementorA.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Structural
{
public class ConcreteImplementorA : Implementor
{
public override void Operation()
{
Console.WriteLine("ConcreteImplementorA Operation");
}
}
}
ConcreteImplementorB.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Structural
{
public class ConcreteImplementorB : Implementor
{
public override void Operation()
{
Console.WriteLine("ConcreteImplementorB Operation");
}
}
}
RefinedAbstraction.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Structural
{
public class RefinedAbstraction : Abstraction
{
public override void Operation()
{
implementor.Operation();
}
}
}
Client.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Structural
{
public class Client
{
static void Main(string[] args)
{
Abstraction abstraction = new RefinedAbstraction(); abstraction.Implementor = new ConcreteImplementorA();
abstraction.Operation(); abstraction.Implementor = new ConcreteImplementorB();
abstraction.Operation();
}
}
}
运行结果:
ConcreteImplementorA Operation
ConcreteImplementorB Operation
请按任意键继续. . .
4、桥接模式实际应用
以一杯咖啡为例,子类有四个:中杯加奶、大杯加奶、中杯不加奶、大杯不加奶。这四个类实际是两个角色的组合:抽象和行为。其中抽象为中杯和大杯,行为为加奶和不加奶。这种从分离抽象和行为的角度的方法称为桥接模式。

MakeCoffee.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Practical
{
public abstract class MakeCoffee
{
public abstract void Making();
}
}
MakeCoffeeSingleton.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Practical
{
/// <summary>
/// 单件模式类用来加载当前MakeCoffee
/// </summary>
public sealed class MakeCoffeeSingleton
{
private static MakeCoffee _instance; public MakeCoffeeSingleton(MakeCoffee instance)
{
_instance = instance;
} public static MakeCoffee Instance()
{
return _instance;
}
}
}
Coffee.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Practical
{
public abstract class Coffee
{
private MakeCoffee _makeCoffee; public Coffee()
{
_makeCoffee = MakeCoffeeSingleton.Instance();
} public MakeCoffee MakeCoffee()
{
return this._makeCoffee;
} public abstract void Make();
}
}
BlackCoffee.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Practical
{
/// <summary>
/// 原味咖啡
/// </summary>
public class BlackCoffee : MakeCoffee
{
public override void Making()
{
Console.WriteLine("原味咖啡");
}
}
}
WhiteCoffee.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Practical
{
/// <summary>
/// 牛奶咖啡
/// </summary>
public class WhiteCoffee : MakeCoffee
{
public override void Making()
{
Console.WriteLine("牛奶咖啡");
}
}
}
MediumCupCoffee.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Practical
{
/// <summary>
/// 中杯
/// </summary>
public class MediumCupCoffee : Coffee
{
public override void Make()
{
MakeCoffee makeCoffee = this.MakeCoffee();
Console.Write("中杯");
makeCoffee.Making();
}
}
}
LargeCupCoffee.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Practical
{
/// <summary>
/// 大杯
/// </summary>
public class LargeCupCoffee : Coffee
{
public override void Make()
{
MakeCoffee makeCoffee = this.MakeCoffee();
Console.Write("大杯");
makeCoffee.Making();
}
}
}
Client.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.BridgePattern.Practical
{
class Client
{
static void Main(string[] args)
{
MakeCoffeeSingleton whiteCoffeeSingleton = new MakeCoffeeSingleton(new WhiteCoffee()); // 中杯牛奶咖啡
MediumCupCoffee mediumWhiteCoffee = new MediumCupCoffee();
mediumWhiteCoffee.Make(); // 大杯牛奶咖啡
LargeCupCoffee largeCupWhiteCoffee = new LargeCupCoffee();
largeCupWhiteCoffee.Make(); MakeCoffeeSingleton blackCoffeeSingleton = new MakeCoffeeSingleton(new BlackCoffee());
// 中杯原味咖啡
MediumCupCoffee mediumBlackCoffee = new MediumCupCoffee();
mediumBlackCoffee.Make(); // 大杯牛奶咖啡
LargeCupCoffee largeCupBlackCoffee = new LargeCupCoffee();
largeCupBlackCoffee.Make();
}
}
}
运行结果:
中杯牛奶咖啡
大杯牛奶咖啡
中杯原味咖啡
大杯原味咖啡
请按任意键继续. . .
5、桥接模式应用分析
桥接模式可以适用于以下情形:
◊ 不希望在抽象与实现部分之间有固定的绑定关系;
◊ 类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。这时桥接模式可以对不同的抽象接口和实现部分进行组合,并分别对它们进行扩充;
◊ 对抽象的实现部分进行修改应对客户不产生影响,即客户的代码不必重新编译;
◊ 想对客户完全隐藏抽象的实现部分;
◊ 想在多个对象间共享实现,但同时要求客户并不知道这点。
桥接模式具有以下特点:
◊ 分离接口及其实现部分,一个实现未必不变地绑定在一个接口上。抽象类的实现可以在运行时刻进行配置,一个对象甚至可以在运行时刻改变它的实现;
◊ 将Abstraction与Implementor分离有助于降低对实现部分编译时刻的依赖性;当改变一个实现类时,并不需要重新编译Abstraction类和Client类。为了保证一个类库的不同版本之间的兼容,需要有这个特性;
◊ 接口与实现分离有助于分层,从而产生更好的结构化系统。系统的高层部分仅需要知道Abstraction和Implementor即可;
◊ 提高可扩充性。可以独立的对Abstraction和Implementor层次结构进行扩充;
◊ 实现细节对Client透明。可以对Client隐藏实现细节,如共享Implementor对象以及相应的引用计数机制。
C#设计模式系列:桥接模式(Bridge)的更多相关文章
- 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern)
原文:乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) 作者:webabcd 介绍 ...
- 【设计模式】桥接模式 Bridge Pattern
开篇还是引用吕振宇老师的那篇经典的文章<设计模式随笔-蜡笔与毛笔的故事>.这个真是太经典了,没有比这个例子能更好的阐明桥接模式了,这里我就直接盗来用了. 现在市面上卖的蜡笔很多,各种型号, ...
- python 设计模式之桥接模式 Bridge Pattern
#写在前面 前面写了那么设计模式了,有没有觉得有些模式之间很类似,甚至感觉作用重叠了,模式并不是完全隔离和独立的,有的模式内部其实用到了其他模式的技术,但是又有自己的创新点,如果一味地认为每个模式都是 ...
- 二十四种设计模式:桥接模式(Bridge Pattern)
桥接模式(Bridge Pattern) 介绍将抽象部分与它的实现部分分离,使它们都可以独立地变化. 示例有一个Message实体类,对它的操作有Insert()和Get()方法,现在使这些操作的抽象 ...
- [设计模式] 7 桥接模式 bridge
#include<iostream> using namespace std; class AbstractionImp { public: virtual ~AbstractionImp ...
- 设计模式之桥接模式(Bridge)--结构模型
1.意图 将抽象部分与它的实现部分分离,使它们可以独立地变化. 2.适用性 你不希望在抽象和它的实现部分之间有一个固定的绑定关系. 类的抽象与它的实现都应该可以通过子类的方式加以扩展. 抽象部分与实现 ...
- 设计模式 笔记 桥接模式 Bridge
//---------------------------15/04/15---------------------------- //Bridge 桥接模式----对象结构型模式 /* 1:意图:将 ...
- 设计模式之桥接模式(Bridge)
桥接模式与原理:将抽象部分与实现部分分离,使它们都可以独立的变化.最终的结果表现在实现类中.两者之间属于等价关系,即实现部分和抽象部分可以相互交换. 代码如下 #include <iostrea ...
- 结构型设计模式之桥接模式(Bridge)
结构 意图 将抽象部分与它的实现部分分离,使它们都可以独立地变化. 适用性 你不希望在抽象和它的实现部分之间有一个固定的绑定关系.例如这种情况可能是因为,在程序运行时刻实现部分应可以被选择或者切换. ...
- 【设计模式】—— 桥接模式Bridge
前言:[模式总览]——————————by xingoo 模式意图 这个模式使用的并不多,但是思想确实很普遍.就是要分离抽象部分与实现部分. 实现弱关联,即在运行时才产生依赖关系. 降低代码之间的耦合 ...
随机推荐
- IE6图片元素img下高度超出出现多余空白
将图片转换为块级元素:display:block; 设置图片的垂直对齐方式:vertical-align属性为top,text-top,bottom,text-bottom 设置父元素的字体大小为0p ...
- JQuery基本知识框架思维导图(上)
一:认识jQuery 1.window.onload与$(document).ready()的对比 2.jQuery代码风格(1:链式代码风格2:位代码添加注释) 3.jQuery对象和DOM对象(1 ...
- 如何在Web引用中使用项目自定义的类
这个是老架构了,不推荐现在这么用,维护一个老项目记录一下. 项目中WebService和客户端是在一个解决方案下,实体类是一个公用的Project,如果使用Web引用自动生成的类会缺少一些实体类定义的 ...
- 原生AJAX封装
var ajaxHelper = { /*1.0 浏览器兼容的方式创建异步对象*/ makeXHR: function () { //声明异步对象变量 var xmlHttp = false; //声 ...
- noi往届题目泛做
noi2015 Day1 t1 程序自动分析 离散化+并查集 t2 软件包管理器 裸树链剖分 t3 寿司晚宴 状压dp Day2 t1 荷马史诗 哈夫曼多叉树 t2 品酒大会 后缀数组按照hei ...
- 来自XP的道别信
当你们看到这封信的时候,很抱歉要和大家说再见了! 亲爱的你们,对不起,请不要为我哭泣,请让我们一起度过这最后的时光. 请记住那个在蓝天白云下奔跑的我!
- weex append
append有两个值:其中的一个是tree, 另外一个是node. 不会像数据绑定一样对最后的渲染结果有影响.但它决定是否会影响整个节点的重绘还是只是某一个地方的内容会重绘. append=" ...
- java分享第十九天(TestNg的IReporter接口的使用)
IReporter接口是干嘛的?就是让用户自定义报告的,很多人想要自定义报告,于是乎找各种插件,比如什么testng-xslt啊,reportng啊,各种配置,最后出来的结果,还不能定制化,但为什么 ...
- 使用mybatis-generator生成代码
文档地址: http://mbg.cndocs.tk/index.html 以下是一个简单的配置内容. 一.在maven配置文件中添加mybatis-generator插件 1 2 3 4 5 ...
- js中的navigator对象
Navigator 对象包含有关浏览器的信息.所有浏览器都支持该对象 在控制台中输出相关信息的代码 <script> console.log(navigator); </script ...