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.
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.
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.
- Separates Common and specific functionality
- Increases Cohesiveness
- Removes inheritance (hierarchies)
- Removes tight coupling
- Reduces the number of classes