Bridge Pattern

696 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
696
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
32
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Bridge Pattern

  1. 1. Design Pattern Training Bridge Pattern by Somenath Mukhopadhyay som@som-itsolutions.com
  2. 2. PurposeTo decouple abstraction from implementation sothat the two can be extended/reusedindependently
  3. 3. Class Diagram
  4. 4. Applicability Helps us in avoiding a permanent bindingbetween an abstraction and implementation It might be useful if we want to select/switch theimplementation at runtime Abstraction and implementation can be extendedindependently. Different abstraction can beattached to different implementation
  5. 5. Applicability The client cannot know anything about theimplementation. Hence if the implementation ischanged, the client code will not be recompiled
  6. 6. Example We have two classes – Window and WindowImp Window class has got two subclasses –IconWindow and TransientWindow WindowImp class has got two subclasses –XwindowImp and PMWindowImp
  7. 7. Example Window class realizes its functionalities throughthe functions of WindowImp class
  8. 8. Example - Window.hclass Window{public: Window(); WindowImp* GetWindowImp(const int type_of_implementation ); void DrawRect(const Coord& aTopLeft, const Coord& aBottomRight); void DrawLine(const Coord& aBegin, const Coord& aEnd);private: WindowImp* imp;};class IconWindow : public Window{public: IconWindow(); void DrawBorder(const Coord& aTopLeft, const Coord& aBottomRight);};class TransientWindow : public Window{public: TransientWindow();};
  9. 9. Explanation – Window class It has got the basic functionalities like DrawRectand DrawLineIt has got GetWindowImp to bind a specificWindow to another specific implementationThe IconWindow class has its own functionDrawBorder which is implemented through theWindow class functions TransientWindow class just uses Window classfunctions
  10. 10. WindowImp classclass WindowImp{public: WindowImp(); virtual void DeviceLine(const Coord& aBeginningPoint, const Coord& aEndPoint) = 0; virtual void DeviceRect(const Coord& aTopLeft, const Coord& aBottomRight) = 0;};class XWindowImp : public WindowImp{public: XWindowImp(); virtual void DeviceLine(const Coord& aBeginningPoint, const Coord& aEndPoint); virtual void DeviceRect(const Coord& aTopLeft, const Coord& aBottomRight);};class PMWindowImp : public WindowImp{public: PMWindowImp(); virtual void DeviceLine(const Coord& aBeginningPoint, const Coord& aEndPoint); virtual void DeviceRect(const Coord& aTopLeft, const Coord& aBottomRight);};
  11. 11. Explanation – WindowImp class Its an abstract class having two pure virtualfunctions Two subclasses have been derived from it –XWindowImp and PMWindowImp These two subclasses override the base classfunctions according to the specific needs
  12. 12. WindowImp classWindowImp::WindowImp() {}XWindowImp::XWindowImp(){}PMWindowImp::PMWindowImp() {}void XWindowImp::DeviceLine(const Coord& aBeginningPoint, const Coord& aEndPoint){ cout<<"Draw the XWindow version of Line draw"<<endl;}void XWindowImp::DeviceRect(const Coord &aTopLeft, const Coord &aBottomRight){ cout<<"Draw the XWindow version of Rectangle draw"<<endl;}void PMWindowImp::DeviceLine(const Coord &aBeginningPoint, const Coord &aEndPoint){ cout<<"Draw the PMWindow version of Line draw"<<endl;}void PMWindowImp::DeviceRect(const Coord &aTopLeft, const Coord &aBottomRight){ cout<<"Draw the PMWindow version of Rectangle draw"<<endl;}
  13. 13. Window classWindow::Window(){ imp = 0;}WindowImp* Window::GetWindowImp(const int type_of_implementation){ imp = WindowSystemFactory::Instance()-> MakeWindowImp(type_of_implementation); return imp;}void Window::DrawLine(const Coord& aBegin, const Coord& aEnd){ imp->DeviceLine(aBegin, aEnd);}void Window::DrawRect(const Coord &aTopLeft, const Coord &aBottomRight){ imp->DeviceRect(aTopLeft,aBottomRight);}IconWindow::IconWindow() {}void IconWindow::DrawBorder(const Coord& aTopLeft, const Coord& aBottomRight){ DrawRect(aTopLeft, aBottomRight);}TransientWindow::TransientWindow() {}
  14. 14. Window class It creates the specific implementation classthrough the factory class WindowSystemFactory
  15. 15. WindowSystemFactory Classclass WindowImp;class WindowSystemFactory{public: static WindowSystemFactory* Instance(); WindowImp* MakeWindowImp(const int type_of_implementation );private: WindowSystemFactory(); static WindowSystemFactory* instance;};
  16. 16. WindowSystemFactory classWindowSystemFactory* WindowSystemFactory::instance = 0;WindowSystemFactory::WindowSystemFactory() {}WindowSystemFactory* WindowSystemFactory::Instance(){ if (NULL = = instance) { instance = new WindowSystemFactory; } return instance;}WindowImp* WindowSystemFactory::MakeWindowImp(const int type_of_implementation){ if (type_of_implementation == XwindowImplementation) return new XwindowImp; if (type_of_implementation == PMWindowImplementation) return new PMWindowImp; return 0;}
  17. 17. Client of Abstraction IconWindow* XIconWindow = new IconWindow; XIconWindow->GetWindowImp(XWindowImplementation); const Coord pt0(1,2); const Coord pt1(7,8); XIconWindow->DrawBorder(pt0,pt1); TransientWindow* PMTransientWindow = new TransientWindow; PMTransientWindow->GetWindowImp(PMWindowImplementation); PMTransientWindow->DrawRect(pt0,pt1); IconWindow* PMIconWindow = new IconWindow; PMIconWindow->GetWindowImp(PMWindowImplementation); PMIconWindow->DrawLine(pt0,pt1);}
  18. 18. Explanation There are two Window objects – XIconWindowand PMTransientWindow As the name suggests XiconWindow will createan IconWindow object and it will attach itself to anXWindowImp implementation On the other hand PMTransientWindow willcreate a TransientWindow object and attach itselfto an PMWindowImp implementation
  19. 19. Explanation The attachment of an Window object to a specificImplementation is done through GetWindowImpfunction This function takes the help of theWindowSystemFactory class to create specificimplementation
  20. 20. Explanation Similarly we can have one IconWindow objectand attach it to a PMWindowImp implementationor a TransientWindow object and attach it to aXwindowImp implementation
  21. 21. contd... If there is only one implementation, there is noneed to create an abstract Implementor class. This is only one-to-one relationship between theabstraction and its implementation However, it helps us in avoiding the recompilationof the client code when the implementation classis changed
  22. 22. contd... In the example, we have attached an abstractionto its implementation by taking the help of theWindowSystemFactory class This can also be achieved in the constructor ofthe Window class. For example the window classcan instantiate the specific implementationdepending on the parameter passed to itsconstructor

×