Design Pattern means that “we had some problem then we found the solution and gave name to this solution” and Bridge is also one of the solution so we need to find the problem that Bridge has solved and how ?
This design pattern comes under Structural Category .
================================================================
C++ Bridge Design Pattern :
Standard Definition:
Bridge Design Pattern decouple an abstraction from implementation so that both(interface and implementation) can vary independently.
In layman language:
It is same like actual bridge that connect two things (it connects interface and implementation).
Problem:
Don’t want to do tight coupling between interface and implementation and we have many interface and user wants to select one at run time.
Solution:
Provide two hierarchy, one for interface and one for implementation, both can vary independently and we can select the right (required based on need) interface and implementation during run time and they will communicate and will do the operation, this solution called as “bridge design pattern“.
When we will use bridge design pattern:
- When we have many interface and many implementation.
- we want to select the interface during run time (dynamic interface is needed).
Advantage:
- Many interfaces are available and its present in separate hierarchy so easy to add new relevant interface.
- Many implementation interface are available and its present in separate hierarchy so easy to add new relevant implantation.
- User can select any interface at run time (dynamic interface).
- Loosely coupled so easy to extend.
- We can add new interface or implementation at any time without polluting existing code.
Note (Interview Tip):
If interviewer ask anything related many interface/dynamic interface or give any problem related to multiple interface then he/she wants to listen about Bridge Design pattern however If he/she ask about interface conflict or legacy interface then he/she wants to listen about Adapter Design patter so please take care of this point.
Class Diagram:
Class Diagram of real example:
Source code of real example in C++:
/* * File:bridge.cpp * This file is described the Bridge Design Pattern with help of example * Author: Alpha * Date: 3 Jan 2021 */</pre> //Header File #include<iostream> //Window Implementor class WindowImp { public: virtual void DrawOnMobile() = 0; virtual void DrawOnDesktop() = 0; }; class Window { protected: WindowImp* imp; public: Window(WindowImp* w):imp(w){} virtual ~Window(){} virtual void Draw() = 0; }; class MobileWindow: public Window { public: MobileWindow(WindowImp* w): Window(w){std::cout<<"Need Mobile Window"<<std::endl;} ~MobileWindow(){} void Draw() { imp->DrawOnMobile(); } }; class DesktopWindow:public Window { public: DesktopWindow(WindowImp* w): Window(w){std::cout<<"Need Desktop Window"<<std::endl;} ~DesktopWindow(){} void Draw() { imp->DrawOnDesktop(); } }; class LinuxImplementor: public WindowImp { public: LinuxImplementor(){std::cout<<"Linux OS has been selected"<<std::endl;} ~LinuxImplementor(){} void DrawOnMobile() { std::cout<<"Linux OS for Mobile"<<std::endl; } void DrawOnDesktop() { std::cout<<"Linux OS for Desktop"<<std::endl; } }; class MacImplementor: public WindowImp { public: MacImplementor(){std::cout<<"Mac OS has been selected"<<std::endl;} ~MacImplementor(){} void DrawOnMobile() { std::cout<<"Mac OS for Mobile"<<std::endl; } void DrawOnDesktop() { std::cout<<"Mac OS for Desktop"<<std::endl; } }; int main() { std::cout<<"Bridge Design Pattern"<<std::endl; WindowImp* imp = new LinuxImplementor; MobileWindow mob(imp); mob.Draw(); delete imp; return 0; };
Compilation command:
g++ -std=c++11 bridge.cpp
Output:
Bridge Design Pattern
Linux OS has been selected
Need Mobile Window
Linux OS for Mobile