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.
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.
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.