0% found this document useful (0 votes)
38 views5 pages

Bridge Design Pattern Explained

The Bridge pattern decouples an abstraction from its implementation so that the two can vary independently. It does this by decomposing an object's interface and implementation into separate class hierarchies. The interface class contains a reference to the implementation class and delegates requests to it. This allows the abstraction and implementation to be extended independently without affecting each other. The Bridge pattern is useful when there is a need for loose coupling between abstractions and implementations to avoid a tightly bound hierarchy.

Uploaded by

Daniyal Mughal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views5 pages

Bridge Design Pattern Explained

The Bridge pattern decouples an abstraction from its implementation so that the two can vary independently. It does this by decomposing an object's interface and implementation into separate class hierarchies. The interface class contains a reference to the implementation class and delegates requests to it. This allows the abstraction and implementation to be extended independently without affecting each other. The Bridge pattern is useful when there is a need for loose coupling between abstractions and implementations to avoid a tightly bound hierarchy.

Uploaded by

Daniyal Mughal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

(/) / DesignPatterns(/design_patterns) / Structuralpatterns

(/design_patterns/structural_patterns)

BridgeDesignPattern
Intent
Decoupleanabstractionfromitsimplementationsothatthetwocanvaryindependently.
Publishinterfaceinaninheritancehierarchy,andburyimplementationinitsown
inheritancehierarchy.
Beyondencapsulation,toinsulation

Problem
"Hardeningofthesoftwarearteries"hasoccurredbyusingsubclassingofanabstractbase
[Link]
[Link]
extendedorcomposed.

Motivation
Considerthedomainof"threadscheduling".

Therearetwotypesofthreadschedulers,andtwotypesofoperatingsystemsor"platforms".
Giventhisapproachtospecialization,wehavetodefineaclassforeachpermutationofthese
[Link](say...Java'sVirtualMachine),whatwouldour
hierarchylooklike?

Whatifwehadthreekindsofthreadschedulers,andfourkindsofplatforms?Whatifwehad
fivekindsofthreadschedulers,andtenkindsofplatforms?Thenumberofclasseswewould
havetodefineistheproductofthenumberofschedulingschemesandthenumberof
platforms.
TheBridgedesignpatternproposesrefactoringthisexponentiallyexplosiveinheritance
hierarchyintotwoorthogonalhierarchiesoneforplatformindependentabstractions,and
theotherforplatformdependentimplementations.

Discussion
Decomposethecomponent'sinterfaceandimplementationintoorthogonalclasshierarchies.
[Link]
initializedwithaninstanceofaconcreteimplementationclass,butallsubsequentinteraction
fromtheinterfaceclasstotheimplementationclassislimitedtotheabstractionmaintainedin
[Link],anditinturn
"delegates"allrequeststotheimplementationclass.
Theinterfaceobjectisthe"handle"knownandusedbytheclientwhiletheimplementation
object,or"body",issafelyencapsulatedtoensurethatitmaycontinuetoevolve,orbe
entirelyreplaced(orsharedatruntime.
UsetheBridgepatternwhen:
youwantruntimebindingoftheimplementation,

youhaveaproliferationofclassesresultingfromacoupledinterfaceandnumerous
implementations,
youwanttoshareanimplementationamongmultipleobjects,
youneedtomaporthogonalclasshierarchies.
Consequencesinclude:
decouplingtheobject'sinterface,
improvedextensibility(youcanextend([Link])theabstractionandimplementation
hierarchiesindependently),
hidingdetailsfromclients.
Bridgeisasynonymforthe"handle/body"[Link]
[Link],
[Link],butthe
workisdoneinthebody."Thehandle/bodyclassidiommaybeusedtodecomposea
complexabstractionintosmaller,[Link]
sharingofasingleresourcebymultipleclassesthatcontrolaccesstoit([Link]
counting)."

Structure
[Link]
encapsulatesthiscomplexitybehindanabstraction"wrapper".
Bridgeemphasizesidentifyinganddecoupling"interface"abstractionfrom"implementation"
abstraction.

Example
TheBridgepatterndecouplesanabstractionfromitsimplementation,sothatthetwocanvary
[Link],ceilingfans,[Link]
[Link]
implementedasapullchain,simpletwopositionswitch,oravarietyofdimmerswitches.

Checklist
1. [Link]

couldbe:abstraction/platform,ordomain/infrastructure,orfrontend/backend,or
interface/implementation.
2. Designtheseparationofconcerns:whatdoestheclientwant,andwhatdotheplatforms
provide.
3. Designaplatformorientedinterfacethatisminimal,necessary,[Link]
decoupletheabstractionfromtheplatform.
4. Defineaderivedclassofthatinterfaceforeachplatform.
5. Createtheabstractionbaseclassthat"hasa"platformobjectanddelegatestheplatform
orientedfunctionalitytoit.
6. Definespecializationsoftheabstractionclassifdesired.

Rulesofthumb
Adaptermakesthingsworkafterthey'redesignedBridgemakesthemworkbeforethey
are.
Bridgeisdesignedupfronttolettheabstractionandtheimplementationvary
[Link].
State,Strategy,Bridge(andtosomedegreeAdapter)havesimilarsolutionstructures.
Theyallshareelementsofthe"handle/body"[Link],they
solvedifferentproblems.
ThestructureofStateandBridgeareidentical(exceptthatBridgeadmitshierarchiesof
envelopeclasses,whereasStateallowsonlyone).Thetwopatternsusethesame
structuretosolvedifferentproblems:Stateallowsanobject'sbehaviortochangealong
withitsstate,whileBridge'sintentistodecoupleanabstractionfromitsimplementationso
thatthetwocanvaryindependently.
Ifinterfaceclassesdelegatethecreationoftheirimplementationclasses(insteadof
creating/couplingthemselvesdirectly),thenthedesignusuallyusestheAbstractFactory
patterntocreatetheimplementationobjects.

Readnext
ThisarticleistakenfromourbookDesignPatternsExplainedSimply(/designpatterns
book).
[Link],simplelanguage
thatmakesiteasytoreadandunderstand(justlikethisarticle).

You might also like