原版设计模式之桥接模式

Intent (定义)

Decouple an abstraction from its implementation so that the two can vary independently. (从实现中分离出抽象,使两者可以独立变化

Also Known As (别名)

Handle/Body

Motivation (详细解释)

在这里插入图片描述
如上图,如果要让程序在X系统和IBM的演示系统(PM)上都顺利运行的话,那么需要定义XWindow和PMWindow两个窗口展示。
假设出现一个图标窗口(IconWindow),那么为了让程序能在X系统和PM系统上展示,就需要实现XIconWindow和PMIconWindow。
这种扩展是无止境和不可预料的。
The Bridge pattern addresses these problems by putting the Window abstraction and its implementation in separate class hierarchies.(桥接模式通过将Window的抽象,和它的实现放在单独的层次结构中来解决这个问题
There is one class hierarchy for window interfaces (Window, IconWindow, TransientWindow) and a separate hierarchy for platform-specific window implementations, with WindowImp as its root.(一个类作为窗口接口(Window,IconWindow,TransientWindow),另一个独立的接口以WindowImp作为根,成为特殊平台的窗口实现
The XWindowImp subclass, for example, provides an implementation based on the X Window System.(例如,XWindowImp这个子类,提供X系统的实现

在这里插入图片描述
All operations on Window subclasses are implemented in terms of abstract operations from the WindowImp interface.(所有在Window子类的操作都是根据WindowImp接口的抽象操作实现的
This decouples the window abstractions from the various platform-specific implementations.(这就将不同平台的实现和窗口抽象解耦了
We refer to the relationship between Window and WindowImp as a bridge, because it bridges the abstraction and its implementation, letting them vary independently.(我们将Window和WindowImp之间的关系称为bridge,因为它连接了抽象和具体实现,并让它们独立变化
参考代码

Applicability (适用点)

  • you want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time(你希望避免抽象和具体实现永久绑定,比如在运行中切换或者选择具体的实现。
  • both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently.(抽象和实现都应该可以被子类扩展,在这种情况下,桥接模式允许你组合不同的抽象和具体实现,并可以独立的扩展
  • changes in the implementation of an abstraction should have no impact on clients; that is, their code should not have to be recompiled.(抽象实现的更改应该对客户端是无影响的,因此,【客户端】的代码是不需要重新编译的。
  • (C++) you want to hide the implementation of an abstraction completely from clients. In C++ the representation of a class is visible in the class interface.(C++语言中,你希望能对客户端隐藏抽象的实现。在C++中,类的展示在类接口中是可见的。
  • you have a proliferation of classes as shown earlier in the first Motivation diagram. Such a class hierarchy indicates the need for splitting an object into two parts. Rumbaugh uses the term “nested generalizations” [RBP+91] to refer to such class hierarchies.(如上图展示的,你需要扩充类。这种类层次结构表明,需要将一个对象分解成两部分。Rumbaugh 用术语‘嵌套泛化’在表示这种类结构。
  • you want to share an implementation among multiple objects (perhaps using reference counting), and this fact should be hidden from the client. A simple example is Coplien’s String class [Cop92], in which multiple objects can share the same string representation (StringRep).(你想要在多个对象间共享实现,比如用在程序计数器中,这种方案应该对客户端是隐藏的。例如 Coplien写的String类,多个对象间指向同一个字符串

Structure (结构)

在这里插入图片描述

Participants (类说明)

  • Abstraction (Window) (抽象
    • defines the abstraction’s interface. (定义抽象接口
    • maintains a reference to an object of type Implementor.(维护具体实现对象的引用
  • RefinedAbstraction (IconWindow) (具体抽象
    • Extends the interface defined by Abstraction.(扩展抽象接口
  • Implementor (WindowImp) (实现者
    • defines the interface for implementation classes.(定义实现类的接口) This interface doesn’t have to correspond exactly to Abstraction’s interface;(这个接口不必对应具体的抽象接口) in fact the two interfaces can be quite different. (实际上,这两个接口可能完全不同)Typically the Implementor interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives.(实际上,实现接口仅仅提供基本的操作,而抽象定义了基础操作之上更高级别的操作。
  • ConcreteImplementor (XWindowImp, PMWindowImp) (具体实现
    • implements the Implementor interface and defines its concrete
      implementation.(继承实现接口,并实现具体操作。

Collaborations (约定)

Abstraction forwards client requests to its Implementor object.(抽象将客户端的请求转发给实现对象。

Related Patterns (相关模式)

An Abstract Factory (99) can create and configure a particular Bridge.(抽象工厂可以创建和配置特殊的桥
The Adapter (157) pattern is geared toward making unrelated classes work together. It is usually applied to systems after they’re designed. (适配器模式是使不相关的类一起协作,通常是应用在系统完成后
Bridge, on the other hand, is used up-front in a design to let abstractions and implementations vary independently.(而桥接模式是预先在设计系统时使用,以让抽象和实现独立变化。
【适配器,对接已经完成的系统,需要拥抱后续变化;桥接,在设计系统时就要考虑。前者在系统已经完成,后者是系统还在设计。】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值