Bridge Design Pattern [Structural]

Why? Increasing feature set explode class hierarchies. The Bridge pattern dissolves complex class hierarchies by separating common and specific functionality into different hierarchies.

In a typical software development as we add new features, the number of subclasses increases at an alarming rate. By decoupling an implementation from an interface, both can vary independently.

Goal is to separate out common functionality and specific ones.

The bridge pattern separates the abstraction from its implementation and reduces the impact of the changes.

Example:

Consider 2 classes – GroupChat & DirectChat. Now if these were in plain chat format. Then you get a requirement to add support for Secure Chat and Self-destructing chat, now general tendency is to extend the each chat into 2 others SecureGroupChat and SelfDestructGroupChat also SecureDirectChat and SelfDestructDirectChat. Now consider if there is another type of chat – BroadcastChat and we need to extend this to SecureBroadcastChat and SelfDestructBroadcastChat. Now we have a total of 9 classes.

Brdge pattern – solves this with interfaces and a bridge between the interface.

Bridge.jpg

Using bridge pattern all Chat classes implement ChatInterface which will include a MessageInterface. On the other side all 3 Secure, SelfDestruct and Plain Message types will conform to MessageInterface.

Advantages:

  1. Separates Common and specific functionality
  2. Increases Cohesiveness
  3. Removes inheritence (hierarchies)
  4. Removes tight coupling
  5. Reduces number of classes

 

One thought on “Bridge Design Pattern [Structural]

Leave a Reply