SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Downloaden Sie, um offline zu lesen
Design Pattern Training


 Bridge Pattern
               by


  Somenath Mukhopadhyay

     som@som-itsolutions.com
Purpose



To decouple abstraction from implementation so
that the two can be extended/reused
independently
Class Diagram
Applicability


 Helps us in avoiding a permanent binding
between an abstraction and implementation

 It might be useful if we want to select/switch the
implementation at runtime

 Abstraction and implementation can be extended
independently. Different abstraction can be
attached to different implementation
Applicability




 The client cannot know anything about the
implementation. Hence if the implementation is
changed, the client code will not be recompiled
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
Example





 Window class realizes its functionalities through
the functions of WindowImp class
Example - Window.h
class 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();
};
Explanation – Window class

 It has got the basic functionalities like DrawRect
and DrawLine

It has got GetWindowImp to bind a specific



Window to another specific implementation

The IconWindow class has its own function



DrawBorder which is implemented through the
Window class functions

  TransientWindow class just uses Window class
functions
WindowImp class
class 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);
};
Explanation – WindowImp class


 Its an abstract class having two pure virtual
functions

 Two subclasses have been derived from it –
XWindowImp and PMWindowImp

 These two subclasses override the base class
functions according to the specific needs
WindowImp class
WindowImp::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;

}
Window class
Window::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() {}
Window class





 It creates the specific implementation class
through the factory class WindowSystemFactory
WindowSystemFactory Class

class WindowImp;

class WindowSystemFactory
{
public:
     static WindowSystemFactory* Instance();
     WindowImp* MakeWindowImp(const int type_of_implementation );

private:
     WindowSystemFactory();
     static WindowSystemFactory* instance;
};
WindowSystemFactory class
WindowSystemFactory* 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;
}
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);
}
Explanation


 There are two Window objects – XIconWindow
and PMTransientWindow

 As the name suggests XiconWindow will create
an IconWindow object and it will attach itself to an
XWindowImp implementation

 On the other hand PMTransientWindow will
create a TransientWindow object and attach itself
to an PMWindowImp implementation
Explanation



 The attachment of an Window object to a specific
Implementation is done through GetWindowImp
function

 This function takes the help of the
WindowSystemFactory class to create specific
implementation
Explanation




 Similarly we can have one IconWindow object
and attach it to a PMWindowImp implementation
or a TransientWindow object and attach it to a
XwindowImp implementation
contd...


 If there is only one implementation, there is no
need to create an abstract Implementor class.

 This is only one-to-one relationship between the
abstraction and its implementation

 However, it helps us in avoiding the recompilation
of the client code when the implementation class
is changed
contd...


 In the example, we have attached an abstraction
to its implementation by taking the help of the
WindowSystemFactory class

  This can also be achieved in the constructor of
the Window class. For example the window class
can instantiate the specific implementation
depending on the parameter passed to its
constructor

Weitere ähnliche Inhalte

Was ist angesagt?

Creational pattern
Creational patternCreational pattern
Creational patternHimanshu
 
PATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design PatternsPATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design PatternsMichael Heron
 
Design Patterns By Sisimon Soman
Design Patterns By Sisimon SomanDesign Patterns By Sisimon Soman
Design Patterns By Sisimon SomanSisimon Soman
 
Design Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator PatternDesign Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator Patterneprafulla
 
SAD11 - Sequence Diagrams
SAD11 - Sequence DiagramsSAD11 - Sequence Diagrams
SAD11 - Sequence DiagramsMichael Heron
 
PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsMichael Heron
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1Shahzad
 
Structural Design pattern - Adapter
Structural Design pattern - AdapterStructural Design pattern - Adapter
Structural Design pattern - AdapterManoj Kumar
 
Pertemuan 6-2-sequence-diagram
Pertemuan 6-2-sequence-diagramPertemuan 6-2-sequence-diagram
Pertemuan 6-2-sequence-diagramAbi Bobon
 
Constructor destructor.ppt
Constructor destructor.pptConstructor destructor.ppt
Constructor destructor.pptKarthik Sekar
 
Design pattern reading group – decorator pattern
Design pattern reading group – decorator patternDesign pattern reading group – decorator pattern
Design pattern reading group – decorator patternVincent Chu
 

Was ist angesagt? (20)

Creational pattern
Creational patternCreational pattern
Creational pattern
 
PATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design PatternsPATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design Patterns
 
Design Patterns By Sisimon Soman
Design Patterns By Sisimon SomanDesign Patterns By Sisimon Soman
Design Patterns By Sisimon Soman
 
Composite design pattern
Composite design patternComposite design pattern
Composite design pattern
 
Design Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator PatternDesign Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator Pattern
 
SAD11 - Sequence Diagrams
SAD11 - Sequence DiagramsSAD11 - Sequence Diagrams
SAD11 - Sequence Diagrams
 
PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Sequence diagrams in UML
Sequence diagrams in UMLSequence diagrams in UML
Sequence diagrams in UML
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Structural Design pattern - Adapter
Structural Design pattern - AdapterStructural Design pattern - Adapter
Structural Design pattern - Adapter
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 
Decorator Pattern
Decorator PatternDecorator Pattern
Decorator Pattern
 
Facade Design Pattern
Facade Design PatternFacade Design Pattern
Facade Design Pattern
 
Pertemuan 6-2-sequence-diagram
Pertemuan 6-2-sequence-diagramPertemuan 6-2-sequence-diagram
Pertemuan 6-2-sequence-diagram
 
Constructor destructor.ppt
Constructor destructor.pptConstructor destructor.ppt
Constructor destructor.ppt
 
Design pattern reading group – decorator pattern
Design pattern reading group – decorator patternDesign pattern reading group – decorator pattern
Design pattern reading group – decorator pattern
 
Visitor pattern
Visitor patternVisitor pattern
Visitor pattern
 

Andere mochten auch

20120420 - Design pattern bridge
20120420 - Design pattern bridge20120420 - Design pattern bridge
20120420 - Design pattern bridgeLearningTech
 
Chain of responsibility
Chain of responsibilityChain of responsibility
Chain of responsibilityShakil Ahmed
 
Chain of Responsibility Pattern
Chain of Responsibility PatternChain of Responsibility Pattern
Chain of Responsibility PatternHüseyin Ergin
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Patternguy_davis
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design PatternShahriar Hyder
 
Descriptive statistics
Descriptive statisticsDescriptive statistics
Descriptive statisticsRajesh Gunesh
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design PatternAdeel Riaz
 
Implementing the Adapter Design Pattern
Implementing the Adapter Design PatternImplementing the Adapter Design Pattern
Implementing the Adapter Design PatternProdigyView
 
Security and Integrity of Data
Security and Integrity of DataSecurity and Integrity of Data
Security and Integrity of DataAdeel Riaz
 

Andere mochten auch (13)

20120420 - Design pattern bridge
20120420 - Design pattern bridge20120420 - Design pattern bridge
20120420 - Design pattern bridge
 
Chain of Responsibility Pattern
Chain of Responsibility PatternChain of Responsibility Pattern
Chain of Responsibility Pattern
 
Chain of responsibility
Chain of responsibilityChain of responsibility
Chain of responsibility
 
Command Pattern
Command PatternCommand Pattern
Command Pattern
 
Command pattern
Command patternCommand pattern
Command pattern
 
Chain of Responsibility Pattern
Chain of Responsibility PatternChain of Responsibility Pattern
Chain of Responsibility Pattern
 
Composite Pattern
Composite PatternComposite Pattern
Composite Pattern
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
 
Descriptive statistics
Descriptive statisticsDescriptive statistics
Descriptive statistics
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Implementing the Adapter Design Pattern
Implementing the Adapter Design PatternImplementing the Adapter Design Pattern
Implementing the Adapter Design Pattern
 
Security and Integrity of Data
Security and Integrity of DataSecurity and Integrity of Data
Security and Integrity of Data
 

Ähnlich wie Design Pattern Training - Bridge Pattern

The Ring programming language version 1.5.4 book - Part 71 of 185
The Ring programming language version 1.5.4 book - Part 71 of 185The Ring programming language version 1.5.4 book - Part 71 of 185
The Ring programming language version 1.5.4 book - Part 71 of 185Mahmoud Samir Fayed
 
Visual Studio tool windows
Visual Studio tool windowsVisual Studio tool windows
Visual Studio tool windowsPVS-Studio
 
The Ring programming language version 1.5.3 book - Part 81 of 184
The Ring programming language version 1.5.3 book - Part 81 of 184The Ring programming language version 1.5.3 book - Part 81 of 184
The Ring programming language version 1.5.3 book - Part 81 of 184Mahmoud Samir Fayed
 
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdfJEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdfMarlouFelixIIICunana
 
The Ring programming language version 1.2 book - Part 51 of 84
The Ring programming language version 1.2 book - Part 51 of 84The Ring programming language version 1.2 book - Part 51 of 84
The Ring programming language version 1.2 book - Part 51 of 84Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 73 of 189
The Ring programming language version 1.6 book - Part 73 of 189The Ring programming language version 1.6 book - Part 73 of 189
The Ring programming language version 1.6 book - Part 73 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 67 of 180
The Ring programming language version 1.5.1 book - Part 67 of 180The Ring programming language version 1.5.1 book - Part 67 of 180
The Ring programming language version 1.5.1 book - Part 67 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 82 of 210
The Ring programming language version 1.9 book - Part 82 of 210The Ring programming language version 1.9 book - Part 82 of 210
The Ring programming language version 1.9 book - Part 82 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 68 of 181
The Ring programming language version 1.5.2 book - Part 68 of 181The Ring programming language version 1.5.2 book - Part 68 of 181
The Ring programming language version 1.5.2 book - Part 68 of 181Mahmoud Samir Fayed
 
Guice tutorial
Guice tutorialGuice tutorial
Guice tutorialAnh Quân
 
Creating a windowed program
Creating a windowed programCreating a windowed program
Creating a windowed programmyrajendra
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1PRN USM
 
L0020 - The Basic RCP Application
L0020 - The Basic RCP ApplicationL0020 - The Basic RCP Application
L0020 - The Basic RCP ApplicationTonny Madsen
 
Windows AI Platform & the Intelligent Edge (pptx)
Windows AI Platform & the Intelligent Edge (pptx)Windows AI Platform & the Intelligent Edge (pptx)
Windows AI Platform & the Intelligent Edge (pptx)Windows Developer
 

Ähnlich wie Design Pattern Training - Bridge Pattern (20)

The Ring programming language version 1.5.4 book - Part 71 of 185
The Ring programming language version 1.5.4 book - Part 71 of 185The Ring programming language version 1.5.4 book - Part 71 of 185
The Ring programming language version 1.5.4 book - Part 71 of 185
 
Visual Studio tool windows
Visual Studio tool windowsVisual Studio tool windows
Visual Studio tool windows
 
The Ring programming language version 1.5.3 book - Part 81 of 184
The Ring programming language version 1.5.3 book - Part 81 of 184The Ring programming language version 1.5.3 book - Part 81 of 184
The Ring programming language version 1.5.3 book - Part 81 of 184
 
GNURAdioDoc-8
GNURAdioDoc-8GNURAdioDoc-8
GNURAdioDoc-8
 
GNURAdioDoc-8
GNURAdioDoc-8GNURAdioDoc-8
GNURAdioDoc-8
 
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdfJEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
 
The Ring programming language version 1.2 book - Part 51 of 84
The Ring programming language version 1.2 book - Part 51 of 84The Ring programming language version 1.2 book - Part 51 of 84
The Ring programming language version 1.2 book - Part 51 of 84
 
manual
manualmanual
manual
 
manual
manualmanual
manual
 
The Ring programming language version 1.6 book - Part 73 of 189
The Ring programming language version 1.6 book - Part 73 of 189The Ring programming language version 1.6 book - Part 73 of 189
The Ring programming language version 1.6 book - Part 73 of 189
 
Rcp by example
Rcp by exampleRcp by example
Rcp by example
 
The Ring programming language version 1.5.1 book - Part 67 of 180
The Ring programming language version 1.5.1 book - Part 67 of 180The Ring programming language version 1.5.1 book - Part 67 of 180
The Ring programming language version 1.5.1 book - Part 67 of 180
 
The Ring programming language version 1.9 book - Part 82 of 210
The Ring programming language version 1.9 book - Part 82 of 210The Ring programming language version 1.9 book - Part 82 of 210
The Ring programming language version 1.9 book - Part 82 of 210
 
The Ring programming language version 1.5.2 book - Part 68 of 181
The Ring programming language version 1.5.2 book - Part 68 of 181The Ring programming language version 1.5.2 book - Part 68 of 181
The Ring programming language version 1.5.2 book - Part 68 of 181
 
Google GIN
Google GINGoogle GIN
Google GIN
 
Guice tutorial
Guice tutorialGuice tutorial
Guice tutorial
 
Creating a windowed program
Creating a windowed programCreating a windowed program
Creating a windowed program
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
 
L0020 - The Basic RCP Application
L0020 - The Basic RCP ApplicationL0020 - The Basic RCP Application
L0020 - The Basic RCP Application
 
Windows AI Platform & the Intelligent Edge (pptx)
Windows AI Platform & the Intelligent Edge (pptx)Windows AI Platform & the Intelligent Edge (pptx)
Windows AI Platform & the Intelligent Edge (pptx)
 

Mehr von Somenath Mukhopadhyay

Significance of private inheritance in C++...
Significance of private inheritance in C++...Significance of private inheritance in C++...
Significance of private inheritance in C++...Somenath Mukhopadhyay
 
Arranging the words of a text lexicographically trie
Arranging the words of a text lexicographically   trieArranging the words of a text lexicographically   trie
Arranging the words of a text lexicographically trieSomenath Mukhopadhyay
 
Generic asynchronous HTTP utility for android
Generic asynchronous HTTP utility for androidGeneric asynchronous HTTP utility for android
Generic asynchronous HTTP utility for androidSomenath Mukhopadhyay
 
Java concurrency model - The Future Task
Java concurrency model - The Future TaskJava concurrency model - The Future Task
Java concurrency model - The Future TaskSomenath Mukhopadhyay
 
Memory layout in C++ vis a-vis polymorphism and padding bits
Memory layout in C++ vis a-vis polymorphism and padding bitsMemory layout in C++ vis a-vis polymorphism and padding bits
Memory layout in C++ vis a-vis polymorphism and padding bitsSomenath Mukhopadhyay
 
Developing an Android REST client to determine POI using asynctask and integr...
Developing an Android REST client to determine POI using asynctask and integr...Developing an Android REST client to determine POI using asynctask and integr...
Developing an Android REST client to determine POI using asynctask and integr...Somenath Mukhopadhyay
 
How to create your own background for google docs
How to create your own background for google docsHow to create your own background for google docs
How to create your own background for google docsSomenath Mukhopadhyay
 
The Designing of a Software System from scratch with the help of OOAD & UML -...
The Designing of a Software System from scratch with the help of OOAD & UML -...The Designing of a Software System from scratch with the help of OOAD & UML -...
The Designing of a Software System from scratch with the help of OOAD & UML -...Somenath Mukhopadhyay
 
Structural Relationship between Content Resolver and Content Provider of Andr...
Structural Relationship between Content Resolver and Content Provider of Andr...Structural Relationship between Content Resolver and Content Provider of Andr...
Structural Relationship between Content Resolver and Content Provider of Andr...Somenath Mukhopadhyay
 
Flow of events during Media Player creation in Android
Flow of events during Media Player creation in AndroidFlow of events during Media Player creation in Android
Flow of events during Media Player creation in AndroidSomenath Mukhopadhyay
 
Implementation of a state machine for a longrunning background task in androi...
Implementation of a state machine for a longrunning background task in androi...Implementation of a state machine for a longrunning background task in androi...
Implementation of a state machine for a longrunning background task in androi...Somenath Mukhopadhyay
 
Tackling circular dependency in Java
Tackling circular dependency in JavaTackling circular dependency in Java
Tackling circular dependency in JavaSomenath Mukhopadhyay
 
Implementation of composite design pattern in android view and widgets
Implementation of composite design pattern in android view and widgetsImplementation of composite design pattern in android view and widgets
Implementation of composite design pattern in android view and widgetsSomenath Mukhopadhyay
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ ConstructorSomenath Mukhopadhyay
 
Active object of Symbian in the lights of client server architecture
Active object of Symbian in the lights of client server architectureActive object of Symbian in the lights of client server architecture
Active object of Symbian in the lights of client server architectureSomenath Mukhopadhyay
 
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
Android Asynctask Internals vis-a-vis half-sync half-async design patternAndroid Asynctask Internals vis-a-vis half-sync half-async design pattern
Android Asynctask Internals vis-a-vis half-sync half-async design patternSomenath Mukhopadhyay
 

Mehr von Somenath Mukhopadhyay (20)

Significance of private inheritance in C++...
Significance of private inheritance in C++...Significance of private inheritance in C++...
Significance of private inheritance in C++...
 
Arranging the words of a text lexicographically trie
Arranging the words of a text lexicographically   trieArranging the words of a text lexicographically   trie
Arranging the words of a text lexicographically trie
 
Generic asynchronous HTTP utility for android
Generic asynchronous HTTP utility for androidGeneric asynchronous HTTP utility for android
Generic asynchronous HTTP utility for android
 
Copy on write
Copy on writeCopy on write
Copy on write
 
Java concurrency model - The Future Task
Java concurrency model - The Future TaskJava concurrency model - The Future Task
Java concurrency model - The Future Task
 
Memory layout in C++ vis a-vis polymorphism and padding bits
Memory layout in C++ vis a-vis polymorphism and padding bitsMemory layout in C++ vis a-vis polymorphism and padding bits
Memory layout in C++ vis a-vis polymorphism and padding bits
 
Developing an Android REST client to determine POI using asynctask and integr...
Developing an Android REST client to determine POI using asynctask and integr...Developing an Android REST client to determine POI using asynctask and integr...
Developing an Android REST client to determine POI using asynctask and integr...
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Uml training
Uml trainingUml training
Uml training
 
How to create your own background for google docs
How to create your own background for google docsHow to create your own background for google docs
How to create your own background for google docs
 
The Designing of a Software System from scratch with the help of OOAD & UML -...
The Designing of a Software System from scratch with the help of OOAD & UML -...The Designing of a Software System from scratch with the help of OOAD & UML -...
The Designing of a Software System from scratch with the help of OOAD & UML -...
 
Structural Relationship between Content Resolver and Content Provider of Andr...
Structural Relationship between Content Resolver and Content Provider of Andr...Structural Relationship between Content Resolver and Content Provider of Andr...
Structural Relationship between Content Resolver and Content Provider of Andr...
 
Flow of events during Media Player creation in Android
Flow of events during Media Player creation in AndroidFlow of events during Media Player creation in Android
Flow of events during Media Player creation in Android
 
Implementation of a state machine for a longrunning background task in androi...
Implementation of a state machine for a longrunning background task in androi...Implementation of a state machine for a longrunning background task in androi...
Implementation of a state machine for a longrunning background task in androi...
 
Tackling circular dependency in Java
Tackling circular dependency in JavaTackling circular dependency in Java
Tackling circular dependency in Java
 
Implementation of composite design pattern in android view and widgets
Implementation of composite design pattern in android view and widgetsImplementation of composite design pattern in android view and widgets
Implementation of composite design pattern in android view and widgets
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ Constructor
 
Active object of Symbian in the lights of client server architecture
Active object of Symbian in the lights of client server architectureActive object of Symbian in the lights of client server architecture
Active object of Symbian in the lights of client server architecture
 
Android services internals
Android services internalsAndroid services internals
Android services internals
 
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
Android Asynctask Internals vis-a-vis half-sync half-async design patternAndroid Asynctask Internals vis-a-vis half-sync half-async design pattern
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
 

Design Pattern Training - Bridge Pattern

  • 1. Design Pattern Training Bridge Pattern by Somenath Mukhopadhyay som@som-itsolutions.com
  • 2. Purpose To decouple abstraction from implementation so that the two can be extended/reused independently
  • 4. Applicability  Helps us in avoiding a permanent binding between an abstraction and implementation  It might be useful if we want to select/switch the implementation at runtime  Abstraction and implementation can be extended independently. Different abstraction can be attached to different implementation
  • 5. Applicability  The client cannot know anything about the implementation. Hence if the implementation is changed, the client code will not be recompiled
  • 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. Example  Window class realizes its functionalities through the functions of WindowImp class
  • 8. Example - Window.h class 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. Explanation – Window class  It has got the basic functionalities like DrawRect and DrawLine It has got GetWindowImp to bind a specific  Window to another specific implementation The IconWindow class has its own function  DrawBorder which is implemented through the Window class functions  TransientWindow class just uses Window class functions
  • 10. WindowImp class class 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. Explanation – WindowImp class  Its an abstract class having two pure virtual functions  Two subclasses have been derived from it – XWindowImp and PMWindowImp  These two subclasses override the base class functions according to the specific needs
  • 12. WindowImp class WindowImp::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. Window class Window::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. Window class  It creates the specific implementation class through the factory class WindowSystemFactory
  • 15. WindowSystemFactory Class class WindowImp; class WindowSystemFactory { public: static WindowSystemFactory* Instance(); WindowImp* MakeWindowImp(const int type_of_implementation ); private: WindowSystemFactory(); static WindowSystemFactory* instance; };
  • 16. WindowSystemFactory class WindowSystemFactory* 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. 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. Explanation  There are two Window objects – XIconWindow and PMTransientWindow  As the name suggests XiconWindow will create an IconWindow object and it will attach itself to an XWindowImp implementation  On the other hand PMTransientWindow will create a TransientWindow object and attach itself to an PMWindowImp implementation
  • 19. Explanation  The attachment of an Window object to a specific Implementation is done through GetWindowImp function  This function takes the help of the WindowSystemFactory class to create specific implementation
  • 20. Explanation  Similarly we can have one IconWindow object and attach it to a PMWindowImp implementation or a TransientWindow object and attach it to a XwindowImp implementation
  • 21. contd...  If there is only one implementation, there is no need to create an abstract Implementor class.  This is only one-to-one relationship between the abstraction and its implementation  However, it helps us in avoiding the recompilation of the client code when the implementation class is changed
  • 22. contd...  In the example, we have attached an abstraction to its implementation by taking the help of the WindowSystemFactory class  This can also be achieved in the constructor of the Window class. For example the window class can instantiate the specific implementation depending on the parameter passed to its constructor