loading...

Bridge Design Pattern [Structural]

itscoderslife profile image Damodar Shenoy Originally published at itscoderslife.wordpress.com on ・1 min read

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

In 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 a plain chat format. Then you get a requirement to add support for Secure Chat and Self-destructing chat, now the general tendency is to extend 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.

Bridge 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 inheritance (hierarchies)
  4. Removes tight coupling
  5. Reduces the number of classes

Posted on by:

itscoderslife profile

Damodar Shenoy

@itscoderslife

Mobile App Developer. Working as iOS developer with Obj-C / Swift. Learning Android development. Full-stack enthusiast. All things tech. Exploring Machine Learning.

Discussion

markdown guide