πAll things you should know about bridge design pattern
βοΈ Motivation
Decouple an abstraction from its implementation so that both can evolve independently. It belongs to the structural category.
π Real-World Scenario
When an abstraction can have multiple implementations, the typical approach is inheritance. But this leads to some issues.
Letβs say we have an abstract class Window, with two implementations: XWindow and PMWindow. If we use inheritance and want to add a new type like IconWindow, weβll need to create XIconWindow and PMIconWindow.
This leads to:
1οΈβ£ Class explosion β For every new window type, we must implement it for each platform.
2οΈβ£ Tight coupling β The client is forced to choose a concrete platform (XWindow or PMWindow), tying the code to a specific implementation.
π― Solution β Bridge Pattern
The Bridge pattern solves this by splitting the abstraction (Window, IconWindow, TransientWindow, etc.) from the implementation (WindowImp, XWindowImp, PMWindowImp, etc.).
π‘ The abstraction holds a reference to the implementation, and they communicate through a defined interface. This bridges the two hierarchies, enabling independent evolution and better flexibility.
π Related Patterns
β’ Abstract Factory can help create and configure a particular Bridge.
β’ Adapter lets unrelated classes work together, but it's applied after design.
β’ Bridge is a design-time decision to decouple abstraction from implementation.
π** Code Example**
See it in action:
π GitHub - https://github.com/shirin-monzavi/BridgeDesignPatternSample
π¬ Your Turn!
βHave you ever used the Bridge Pattern in your projects?
Top comments (0)