Bridge

Bridge

Intent

Decouple an abstraction from its implementation so that the two can vary independently.

Also known as

Handle/Body

Applicability

Use bridge pattern when:

  • You want to avoid a permanent binding between an abstraction and it's implementation. That might be the case, for e.g., when the implementation must be selected or switched at run-time.
  • Both the abstraction and implementation 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, i.e. their code should not have to be recompiled.
  • You want to share an implementation among multiple objects and this fact should be hidden from client.

Structure

Orange

Participants

  • Abstraction: Defines the abstraction's interface, maintains a reference to an object of type Implementator.
  • Refined Abstraction: Extends the Interface defined by Abstraction.
  • Implementor: defines the interface for implementation classes. This interface doesn't have to correspond exactly to Abstraction's interface. Typically the Implementor interface provides only primitive operations, and Abstraction defines higher level operations based on these primitives.
  • ConcreteImplementor: implements the Implementor interface and defines it's concrete implementation.

Collaborations

Abstraction forwards client requests to it's Implementor object.

Consequences

The bridge pattern has following consequences:

  • Decoupling interface and implementation.
  • Improved extensibility.
  • Hiding implementation details from clients.

Sample Code

Bridge

Related Patterns

  • An Abstract Factory can create and configure a particular Bridge.
  • The Adapter pattern is geared toward making unrelated classes work together. It is usually applied to systems after they're designed, whereas Bridge is used up-front.