1、桥接模式简介

1.1>、定义

  当一个抽象可能有多个实现时,通常用继承来进行协调。抽象类定义对该抽象的接口,而具体的子类则用不同的方式加以实现。继承机制将抽象部分与它的实现部分固定在一起,使得难以对抽象部分和实现部分独立地进行修改、扩充和重用。

  如果一个抽象类或接口有多个具体实现子类,而这些子类之中有内容或概念上重叠,需要我们把抽象的共同部分各自独立开来:即原来是准备放在一个接口里,现在需要设计两个接口——抽象接口和行为接口。然后再分别针对各自的具体子类定义抽象接口和行为接口的方法和调用关系。

  桥接模式的用意是将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化。

  抽象化(Abstraction)
  存在于多个实体中的共同的概念性联系,即为抽象化。作为一个过程,抽象化就是忽略一些信息,从而把不同的实体当做同样的实体对待。

  实现化(Implementation)

  抽象化给出的具体实现,即为实现化。

  脱耦
  耦合是指两个对象的行为的某种强关联,脱耦则是要去掉它们之间的强关联。在这里,脱耦是指将抽象化和实现化之间的耦合解脱,或者将它们之间的强关联改换成弱关联。将两者之间的继承关系改为聚合关系,就是将它们之间的强关联改换成为弱关联。
  桥接模式中的脱耦,是指抽象化和实现化之间使用组合/聚合关系,而不是继承关系,从而使两者可以相对独立地变化。

1.2>、使用频率

   中等

2、桥接模式结构图

  桥接模式的结构包括Abstraction、RefinedAbstraction、Implementor、ConcreteImplementorA和ConcreteImplementorB五个部分,其中:

  ◊ Abstraction:定义抽象类的接口,它维护了一个指向Implementor类型对象的指针。

  ◊ RefinedAbstraction:扩充由Abstraction定义的接口;

  ◊ Implementor:定义实现类的接口,该接口不一定要与Abstraction的接口完全一致,事实上两个接口可以完全不同。一般情况,Implementor接口仅为提供基本操作,而Abstraction则定义了基于基本操作的较高层次操作。

  ◊ ConcreteImplementorAConcreteImplementorB:实现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)的更多相关文章

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

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

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

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

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

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

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

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

  5. [设计模式] 7 桥接模式 bridge

    #include<iostream> using namespace std; class AbstractionImp { public: virtual ~AbstractionImp ...

  6. 设计模式之桥接模式(Bridge)--结构模型

    1.意图 将抽象部分与它的实现部分分离,使它们可以独立地变化. 2.适用性 你不希望在抽象和它的实现部分之间有一个固定的绑定关系. 类的抽象与它的实现都应该可以通过子类的方式加以扩展. 抽象部分与实现 ...

  7. 设计模式 笔记 桥接模式 Bridge

    //---------------------------15/04/15---------------------------- //Bridge 桥接模式----对象结构型模式 /* 1:意图:将 ...

  8. 设计模式之桥接模式(Bridge)

    桥接模式与原理:将抽象部分与实现部分分离,使它们都可以独立的变化.最终的结果表现在实现类中.两者之间属于等价关系,即实现部分和抽象部分可以相互交换. 代码如下 #include <iostrea ...

  9. 结构型设计模式之桥接模式(Bridge)

    结构 意图 将抽象部分与它的实现部分分离,使它们都可以独立地变化. 适用性 你不希望在抽象和它的实现部分之间有一个固定的绑定关系.例如这种情况可能是因为,在程序运行时刻实现部分应可以被选择或者切换. ...

  10. 【设计模式】—— 桥接模式Bridge

    前言:[模式总览]——————————by xingoo 模式意图 这个模式使用的并不多,但是思想确实很普遍.就是要分离抽象部分与实现部分. 实现弱关联,即在运行时才产生依赖关系. 降低代码之间的耦合 ...

随机推荐

  1. 记录同事的一个bug-ajax-413错误-fullhead

    症状表现为在form下面的textarea里的字符数只有几十个的时候,请求可以成功,但是如果有几百字,则会出现413错误,提示fullhead,我第一反应是cookie体积太小,但是清了缓存还是一样的 ...

  2. docker学习之二镜像创建

    继上一篇docker入门之后写一点使用的经验. 通过命令:docker run -it REPOSITORY或IMAGE ID   注:-it后面跟的字段可以通过下面指令获得 创建运行的容器,会进入一 ...

  3. Git小记

    Git简~介 Git是一个分布式版本控制系统,其他的版本控制系统我只用过SVN,但用的时间不长.大家都知道,分布式的好处多多,而且分布式已经包含了集中式的几乎所有功能.Linus创造Git的传奇经历就 ...

  4. pyqt 发射接收信号

    翻页控件: 一.定义并发射信号: #!/usr/bin/python # #coding=utf-8 # __author__='' from PyQt4.QtCore import * from P ...

  5. 日常关键字:定时关机、该任务映像已损坏或已篡改.(0x80041321)、ChaZD生词同步扇贝

    我在床上用chinanet网络慢得简直令人发指,12B/S.是的你没有看错,这是我最常看到的网速.但是我最近发现电脑联网开出一个WiFi,在床上用手机上网时,网速会一点提升,可达到1KB/S(⊙﹏⊙) ...

  6. js Date学习

    Date.parse()接收一个表示日期的字符串参数(参数错误时返回NaN),返回相应日期的毫秒数.(使用自 UTC(Coordinated Universal Time,国际协调时间)1970 年 ...

  7. thinkphp 缓存数据

    thinkphp 中内置了缓存操作 3.1版本的数据缓存方法是cache 基本用法: S(array('type'=>'xcache','expire'=>60)); 缓存初始化 缓存初始 ...

  8. JQM开发Tips

    1.radio Button 点击后有时候有高亮样式,有时候没有 解决方案: $("#task_form label").click(function () { $("# ...

  9. (原).NET程序加入多语言包解决方案工具,超级棒

    Multi-Language Add-In Version 5.04.0088 for Visual Studio 2013 安装包:http://www.jollans.com/SetupMulti ...

  10. bzoj2599: [IOI2011]Race(点分治)

    写了四五道点分治的题目了,算是比较理解点分治是什么东西了吧= = 点分治主要用来解决点对之间的问题的,比如距离为不大于K的点有多少对. 这道题要求距离等于K的点对中连接两点的最小边数. 那么其实道理是 ...