Bridge Pattern


The necessary aspect of any good architecture is that it allows components to vary independently. If this decoupling is not achieved, a minor change in the requirement would not a massive change in the implementation. Larger the change in implementation, larger is the effort and so is the possibility of error.

The Bridge Pattern is used to decouple an abstraction from its implementation - so that the two can vary independently.

Here, the interface classes have their own hierarchy as the implementation classes. The base class of the interface class contains the base class of the implementation class. That ensures we can just replace a given module and the rest of the system continues to function without a problem.

Participants


There are 4 major roles in the Bridge Pattern:

  • Abstraction - Defines the abstraction's interface and maintains a reference to an object of type Implementor.
  • RefinedAbstraction - 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; 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 - Implements the Implementor interface and defines its concrete implementation.

The Adapter Pattern we saw before is actually just a formalized code patch to get two independent systems work together. Whereas, bridge is a design that allows the system to grow.