Bridge Design Pattern

bridge design pattern

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

  • 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 Implementation 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 bridge pattern applies when there is a need to avoid permanent binding between an abstraction and an implementation and when the abstraction and implementation need to vary independently. Using the bridge pattern would leave the client code unchanged with no need to recompile the code.
  • Graphical User Interface Frameworks and Persistence Frameworks  use the bridge pattern to separate abstractions from platform specific implementation. For example GUI frameworks separate a Window abstraction from a Window implementation for Linux or Mac OS using the bridge pattern.

  • Abstraction and Implementor hierarchies can be extended independently.
  • Beyond encapsulation, to insulation
  • Run-time binding of the implementation
  • Share an implementation among multiple objects
  • Hiding details from clients
  • Reduction in the number of sub classes - Sometimes, using pure inheritance will increase the number of sub classes and using Bridge Pattern reduces the sub class requirement.
  • Improved extensibility: the extensibility is improved because additional refined abstractions can be created without the need for adding any additional implementations. Likewise, extra concrete implementations can be developed which can be used transparently with the existing and future abstractions.

  • In providing flexibility, it increases complexity. 
  • Double indirection: The abstraction needs to pass messages along to the implementator for the operation to get executed. So, There are possible performance issues with the indirection of messages

Sample Code
Using bridge pattern as an abstraction for different persistences and repository example

Related Patterns
  • Adapter makes things work after they are designed; Bridge makes them work before they are. Bridge is designed up-front to let the abstraction and the implementation vary independently. Adapter is retrofitted to make unrelated classes work together.
  • State, Strategy, Bridge (and to some degree Adapter) have similar solution structures. They all share elements of the “handle/body” idiom. They differ in intent - that is, they solve different problems.
  • The structure of State and Bridge are identical (except that Bridge admits hierarchies of envelope classes, whereas State allows only one). The two patterns use the same structure to solve different problems: State allows an object’s behavior to change along with its state, while Bridge’s intent is to decouple an abstraction from its implementation so that the two can vary independently.
  • The Bridge pattern is a composite of the Template Method and Strategy patterns.
  • If interface classes delegate the creation of their implementation classes (instead of creating/coupling themselves directly), then the design usually uses the Abstract Factory pattern to create the implementation objects.


Further readings


Popular Posts