SlideShare a Scribd company logo
1 of 24
Design Pattern – Bridge
     (Structural)

• INTENT

» DECOUPLE AN ABSTRACTION FROM ITS
IMPLEMENTATION SO THAT THE TWO CAN VARY
INDEPENDENTLY

» SEPARATE THE ABSTRACTION AND ITS
IMPLEMENTATION AND HAVE SEPARATE
INHERITANCE STRUCTURE FOR BOTH.


 • Also known as
 » Handle / Body
                                              by
                            Shahriar Hyder
                                     Oct 05, 2011
Motivation

 Write a program that will draw rectangles with either
 of two drawing programs:
    drawing program 1 (DP1)

    drawing program 2 (DP2)
Example




                DP 1                           DP 2
draw a line     draw_a_line( x1, y1, x2, y2)   drawline( x1, x2, y1, y2)
draw a circle   draw_a_circle( x, y, r)        drawcircle( x, y, r)



The collection (the client of rectangles) doesn’t want to worry
about what type of drawing program it should use.
Cont..

 new requirement:
      asked to support a new shape - circle




     adding another level, called Shape, which
     derives the Rectangle and Circle class.
new problems arise …


  if I get another drawing program – DP3
   •   I will have 6 different kinds of Shapes (two Shape concepts
       times three drawing programs).

  if I get another type of Shape
   •   I will have nine different types of Shapes (three Shape
       concepts times three drawing programs).

                                                  class
DP:Drawing Program S:Shapes
Need ≈DP*S new classes                           explosion!
Want ≈DP+S new classes
The new classes are hard wired for each type of Drawing Programs
Problem

 “Hardening of the software arteries” has occurred by
 using sub-classing of an abstract base class to
 provide alternative implementations. This locks in
 compile-time binding between interface and
 implementation. The abstraction and
 implementation cannot be independently extended
 or composed.
Try another alternate hierarchy




                       redundancy still there !
Step 1. Identify what is varying
Step 2. Represent the variations
Step3. Tie the class together
Step4. Expanding the design
Bridge – Structure
Bridge – Participants

• Abstraction
      » Defines the abstractions interface
      » Maintains a reference to an object of
        type implementor
• RefinedAbstraction
      » Extends the interface defined by
        Abstraction
Bridge – Participants – 2

• Implementer
  » Defines the interface for implementation classes
      > Can be different from the Abstraction interface
          – Implementer provides primitive operations
          – Abstraction provides higher-level operations
• ConcreteImplementer
 » Implements the Implementer interface
 » Defines its concrete implementation
Demo Time!
Bridge – Applicability

• The normal method of dealing with an abstraction having
   several implementations is through inheritance.
• Avoid permanent binding between an abstraction and its
   implementation
   » Especially if selection or switching of implementation is at
   run-time rather than design time.
• Both abstractions and implementations should be extensible
   by sub-classing
• Changes in implementation should have no impact on clients
• Share an implementation among multi objects and this fact
   should be hidden from the client
• You have a proliferation of classes resulting from a coupled
   interface and numerous implementations
• You need to map orthogonal class hierarchies.
Bridge – Applicability Summary

The Bridge pattern is useful when you have an
abstraction that has different implementations. It
allows the abstraction and the implementation to
vary independently of each other.

 • Find what varies and encapsulate it.

 • Favor composition over inheritance.
Collaborations

 Abstraction forwards client requests to its
 Implementor object.
Bridge – Consequences

• Decouples interface and implementation
  » Can configure implementation to use at runtime
  » Encourages better structure through layering
      > The client only has to know about Abstraction
      and Implementer

• Improved extensibility
   » Extend in Abstraction and Implementer
   independently

• Hide implementation details from clients
Implementation

 Ditto from GoF Please!
Bridge – Related Patterns

• Abstract Factory can create and configure a particular Bridge

• Adapter is geared toward making unrelated classes work together
   » Usually applied after systems are designed
   » Bridge is used during design

• Structural difference: Bridge can abstract a complex
   entity from its implementation; Adapter only abstracts a
   single interface

• A bridge is by design. An adaptor is not. An adaptor is
   a patch. A bridge is put in place on purpose.
Thank You

More Related Content

What's hot (20)

Facade pattern
Facade patternFacade pattern
Facade pattern
 
Decorator Pattern
Decorator PatternDecorator Pattern
Decorator Pattern
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Abstract Factory Pattern
Abstract Factory PatternAbstract Factory Pattern
Abstract Factory Pattern
 
Decorator design pattern
Decorator design patternDecorator design pattern
Decorator design pattern
 
Prototype pattern
Prototype patternPrototype pattern
Prototype pattern
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
 
Bridge pattern for Dummies
Bridge pattern for DummiesBridge pattern for Dummies
Bridge pattern for Dummies
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
 
Facade Design Pattern
Facade Design PatternFacade Design Pattern
Facade Design Pattern
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design Pattern
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Facade pattern presentation(.pptx)
Facade pattern presentation(.pptx)Facade pattern presentation(.pptx)
Facade pattern presentation(.pptx)
 
Design Patterns - Factory Method & Abstract Factory
Design Patterns - Factory Method & Abstract FactoryDesign Patterns - Factory Method & Abstract Factory
Design Patterns - Factory Method & Abstract Factory
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
 

Similar to Bridge Design Pattern

Design Pattern lecture 3
Design Pattern lecture 3Design Pattern lecture 3
Design Pattern lecture 3Julie Iskander
 
How to design an application correctly ?
How to design an application correctly ?How to design an application correctly ?
How to design an application correctly ?Guillaume AGIS
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2Julie Iskander
 
Creational Design Patterns.pptx
Creational Design Patterns.pptxCreational Design Patterns.pptx
Creational Design Patterns.pptxSachin Patidar
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)stanbridge
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSergey Aganezov
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxanguraju1
 
Design Patterns Part1
Design Patterns  Part1Design Patterns  Part1
Design Patterns Part1Tom Chen
 
SADP PPTs of all modules - Shanthi D.L.pdf
SADP PPTs of all modules - Shanthi D.L.pdfSADP PPTs of all modules - Shanthi D.L.pdf
SADP PPTs of all modules - Shanthi D.L.pdfB.T.L.I.T
 
Xtext + Sirius = <3
Xtext + Sirius = <3Xtext + Sirius = <3
Xtext + Sirius = <3Cédric Brun
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General IntroductionAsma CHERIF
 
P Training Presentation
P Training PresentationP Training Presentation
P Training PresentationGaurav Tyagi
 
Design pattern and their application
Design pattern and their applicationDesign pattern and their application
Design pattern and their applicationHiệp Tiến
 
Sofwear deasign and need of design pattern
Sofwear deasign and need of design patternSofwear deasign and need of design pattern
Sofwear deasign and need of design patternchetankane
 
Software Architecture and Project Management module III : PATTERN OF ENTERPRISE
Software Architecture and Project Management module III : PATTERN OF ENTERPRISESoftware Architecture and Project Management module III : PATTERN OF ENTERPRISE
Software Architecture and Project Management module III : PATTERN OF ENTERPRISEsreeja_rajesh
 

Similar to Bridge Design Pattern (20)

Design Pattern lecture 3
Design Pattern lecture 3Design Pattern lecture 3
Design Pattern lecture 3
 
How to design an application correctly ?
How to design an application correctly ?How to design an application correctly ?
How to design an application correctly ?
 
Bridge Pattern Derek Weeks
Bridge Pattern   Derek WeeksBridge Pattern   Derek Weeks
Bridge Pattern Derek Weeks
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2
 
Creational Design Patterns.pptx
Creational Design Patterns.pptxCreational Design Patterns.pptx
Creational Design Patterns.pptx
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
 
Ch08lect2 ud
Ch08lect2 udCh08lect2 ud
Ch08lect2 ud
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
Design Patterns Part1
Design Patterns  Part1Design Patterns  Part1
Design Patterns Part1
 
Architectural design
Architectural designArchitectural design
Architectural design
 
SADP PPTs of all modules - Shanthi D.L.pdf
SADP PPTs of all modules - Shanthi D.L.pdfSADP PPTs of all modules - Shanthi D.L.pdf
SADP PPTs of all modules - Shanthi D.L.pdf
 
Xtext + Sirius = <3
Xtext + Sirius = <3Xtext + Sirius = <3
Xtext + Sirius = <3
 
Bridge Pattern
Bridge PatternBridge Pattern
Bridge Pattern
 
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Design pattern and their application
Design pattern and their applicationDesign pattern and their application
Design pattern and their application
 
Sofwear deasign and need of design pattern
Sofwear deasign and need of design patternSofwear deasign and need of design pattern
Sofwear deasign and need of design pattern
 
Software Architecture and Project Management module III : PATTERN OF ENTERPRISE
Software Architecture and Project Management module III : PATTERN OF ENTERPRISESoftware Architecture and Project Management module III : PATTERN OF ENTERPRISE
Software Architecture and Project Management module III : PATTERN OF ENTERPRISE
 

More from Shahriar Hyder

Effective Communication Skills for Software Engineers
Effective Communication Skills for Software EngineersEffective Communication Skills for Software Engineers
Effective Communication Skills for Software EngineersShahriar Hyder
 
A JavaScript Master Class - From the Wows to the WTFs
A JavaScript Master Class - From the Wows to the WTFsA JavaScript Master Class - From the Wows to the WTFs
A JavaScript Master Class - From the Wows to the WTFsShahriar Hyder
 
Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion PrincipleShahriar Hyder
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design PatternShahriar Hyder
 
Taking a Quantum Leap with Html 5 WebSocket
Taking a Quantum Leap with Html 5 WebSocketTaking a Quantum Leap with Html 5 WebSocket
Taking a Quantum Leap with Html 5 WebSocketShahriar Hyder
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming FundamentalsShahriar Hyder
 
Object Relational Mapping with LINQ To SQL
Object Relational Mapping with LINQ To SQLObject Relational Mapping with LINQ To SQL
Object Relational Mapping with LINQ To SQLShahriar Hyder
 
C# 3.0 Language Innovations
C# 3.0 Language InnovationsC# 3.0 Language Innovations
C# 3.0 Language InnovationsShahriar Hyder
 

More from Shahriar Hyder (9)

Effective Communication Skills for Software Engineers
Effective Communication Skills for Software EngineersEffective Communication Skills for Software Engineers
Effective Communication Skills for Software Engineers
 
A JavaScript Master Class - From the Wows to the WTFs
A JavaScript Master Class - From the Wows to the WTFsA JavaScript Master Class - From the Wows to the WTFs
A JavaScript Master Class - From the Wows to the WTFs
 
Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion Principle
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
 
Taking a Quantum Leap with Html 5 WebSocket
Taking a Quantum Leap with Html 5 WebSocketTaking a Quantum Leap with Html 5 WebSocket
Taking a Quantum Leap with Html 5 WebSocket
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
Object Relational Mapping with LINQ To SQL
Object Relational Mapping with LINQ To SQLObject Relational Mapping with LINQ To SQL
Object Relational Mapping with LINQ To SQL
 
C# 3.0 Language Innovations
C# 3.0 Language InnovationsC# 3.0 Language Innovations
C# 3.0 Language Innovations
 
Introduction to Linq
Introduction to LinqIntroduction to Linq
Introduction to Linq
 

Recently uploaded

Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 

Bridge Design Pattern

  • 1. Design Pattern – Bridge (Structural) • INTENT » DECOUPLE AN ABSTRACTION FROM ITS IMPLEMENTATION SO THAT THE TWO CAN VARY INDEPENDENTLY » SEPARATE THE ABSTRACTION AND ITS IMPLEMENTATION AND HAVE SEPARATE INHERITANCE STRUCTURE FOR BOTH. • Also known as » Handle / Body by Shahriar Hyder Oct 05, 2011
  • 2. Motivation  Write a program that will draw rectangles with either of two drawing programs:  drawing program 1 (DP1)  drawing program 2 (DP2)
  • 3. Example DP 1 DP 2 draw a line draw_a_line( x1, y1, x2, y2) drawline( x1, x2, y1, y2) draw a circle draw_a_circle( x, y, r) drawcircle( x, y, r) The collection (the client of rectangles) doesn’t want to worry about what type of drawing program it should use.
  • 4.
  • 5. Cont..  new requirement: asked to support a new shape - circle adding another level, called Shape, which derives the Rectangle and Circle class.
  • 6.
  • 7. new problems arise …  if I get another drawing program – DP3 • I will have 6 different kinds of Shapes (two Shape concepts times three drawing programs).  if I get another type of Shape • I will have nine different types of Shapes (three Shape concepts times three drawing programs). class DP:Drawing Program S:Shapes Need ≈DP*S new classes explosion! Want ≈DP+S new classes The new classes are hard wired for each type of Drawing Programs
  • 8. Problem  “Hardening of the software arteries” has occurred by using sub-classing of an abstract base class to provide alternative implementations. This locks in compile-time binding between interface and implementation. The abstraction and implementation cannot be independently extended or composed.
  • 9. Try another alternate hierarchy redundancy still there !
  • 10. Step 1. Identify what is varying
  • 11. Step 2. Represent the variations
  • 12. Step3. Tie the class together
  • 15. Bridge – Participants • Abstraction » Defines the abstractions interface » Maintains a reference to an object of type implementor • RefinedAbstraction » Extends the interface defined by Abstraction
  • 16. Bridge – Participants – 2 • Implementer » Defines the interface for implementation classes > Can be different from the Abstraction interface – Implementer provides primitive operations – Abstraction provides higher-level operations • ConcreteImplementer » Implements the Implementer interface » Defines its concrete implementation
  • 18. Bridge – Applicability • The normal method of dealing with an abstraction having several implementations is through inheritance. • Avoid permanent binding between an abstraction and its implementation » Especially if selection or switching of implementation is at run-time rather than design time. • Both abstractions and implementations should be extensible by sub-classing • Changes in implementation should have no impact on clients • Share an implementation among multi objects and this fact should be hidden from the client • You have a proliferation of classes resulting from a coupled interface and numerous implementations • You need to map orthogonal class hierarchies.
  • 19. Bridge – Applicability Summary The Bridge pattern is useful when you have an abstraction that has different implementations. It allows the abstraction and the implementation to vary independently of each other. • Find what varies and encapsulate it. • Favor composition over inheritance.
  • 20. Collaborations  Abstraction forwards client requests to its Implementor object.
  • 21. Bridge – Consequences • Decouples interface and implementation » Can configure implementation to use at runtime » Encourages better structure through layering > The client only has to know about Abstraction and Implementer • Improved extensibility » Extend in Abstraction and Implementer independently • Hide implementation details from clients
  • 23. Bridge – Related Patterns • Abstract Factory can create and configure a particular Bridge • Adapter is geared toward making unrelated classes work together » Usually applied after systems are designed » Bridge is used during design • Structural difference: Bridge can abstract a complex entity from its implementation; Adapter only abstracts a single interface • A bridge is by design. An adaptor is not. An adaptor is a patch. A bridge is put in place on purpose.