A Structural Design pattern

Let’s start with the concrete definition, directly from the GoF book, Design Patterns.

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

It states bridge pattern decouples the abstract elements of a class from its implementation by providing a bridge structure between them. Both types of classes can be altered structurally without affecting each other.

It follows the principle which says “prefer composition over inheritance”.

UML Diagram — Four elements of Bridge Pattern(refer : wiki)

Abstraction? Implementation? Sound scary? Stay calm and let’s have a look

  • Abstraction is all about the overall idea what needs to be done whereas Implementation is all about how to achieve it in various circumstances.

Enough of Theories, lets have some practical scenarios.

Requirement 1 : Write a program that can draw Circle and Rectangle.

Solution :

Requirement 2 : Write a program that can draw Circle and Rectangle of Colors Red and Blue.

Solution : –

Requirement 3 : Write a program that can draw Circle and Rectangle of Colors Red, Blue and Green

Solution : –

Solution where Abstraction and Implementation stays together

Here you see, Adding one Color implementation to any shape requires a class addition to each shape type. Think of the scenario where you have N number of Shape types of N different colors….

Grrrr…..

Bridge pattern comes to the rescue.

Solution using Bridge Design Pattern

Therefore, the final program looks like :

When to use the Bridge Pattern:

  • Any Change in implementation should have no impact on abstraction.
  • Your program requires run-time binding of the implementation.
  • You want to share an implementation among multiple objects.
  • When you want to divide and organize a monolithic class that has several variants of some functionality (e.g if the class has to work with various database servers).
  • When you have to develop a module which can have some inherited properties or behaviors along with some dynamic.

Advantages :

  • Decoupling the object’s interface and makes it independent.
  • Improved Extensibility (you can extend (subclass) the abstraction and implementation hierarchies independently).
  • Provides a good solution for cross-platform development.

Does it sound similar to Adapter pattern ?

  • Bridge pattern decouples an abstraction and implementation elements of a class by creating two separate class hierarchies to change independently, whereas an Adapter pattern helps two incompatible classes to work together.
  • Also, as stated by GOF “Adapter makes things work after they’re designed; on the other hand Bridge makes them work before they are.” . It means Adapter pattern is commonly used with already developed application for two incompatible classes to work together . whereas Bridge pattern is usually designed up-front to allow a developer to develop a part of an application and letting other part to be written independently by someone.

Relations with other Design patterns :

  • Bridge, State, Strategy(and to some extent Adapter) have very similar structures(based out of “Composition”) which delegates job to other objects . However, they all are meant to solve different set of problems.

Thanks for reading this article.

You can find the Article on my  Medium blog as well.

Reference :

https://en.wikipedia.org/wiki/Bridge_pattern

Similar Articles

Leave a Reply