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

Bridge Design Pattern

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
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
132 views5 pages

Bridge Design Pattern

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
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

(/) / DesignPatterns(/design_patterns) / Structuralpatterns

(/design_patterns/structural_patterns)

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

Problem
"Hardeningofthesoftwarearteries"hasoccurredbyusingsubclassingofanabstractbase
classtoprovidealternativeimplementations.Thislocksincompiletimebindingbetween
interfaceandimplementation.Theabstractionandimplementationcannotbeindependently
extendedorcomposed.

Motivation
Considerthedomainof"threadscheduling".

Therearetwotypesofthreadschedulers,andtwotypesofoperatingsystemsor"platforms".
Giventhisapproachtospecialization,wehavetodefineaclassforeachpermutationofthese
twodimensions.Ifweaddanewplatform(say...Java'sVirtualMachine),whatwouldour
hierarchylooklike?

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

Discussion
Decomposethecomponent'sinterfaceandimplementationintoorthogonalclasshierarchies.
Theinterfaceclasscontainsapointertotheabstractimplementationclass.Thispointeris
initializedwithaninstanceofaconcreteimplementationclass,butallsubsequentinteraction
fromtheinterfaceclasstotheimplementationclassislimitedtotheabstractionmaintainedin
theimplementationbaseclass.Theclientinteractswiththeinterfaceclass,anditinturn
"delegates"allrequeststotheimplementationclass.
Theinterfaceobjectisthe"handle"knownandusedbytheclientwhiletheimplementation
object,or"body",issafelyencapsulatedtoensurethatitmaycontinuetoevolve,orbe
entirelyreplaced(orsharedatruntime.
UsetheBridgepatternwhen:
youwantruntimebindingoftheimplementation,

youhaveaproliferationofclassesresultingfromacoupledinterfaceandnumerous
implementations,
youwanttoshareanimplementationamongmultipleobjects,
youneedtomaporthogonalclasshierarchies.
Consequencesinclude:
decouplingtheobject'sinterface,
improvedextensibility(youcanextend(i.e.subclass)theabstractionandimplementation
hierarchiesindependently),
hidingdetailsfromclients.
Bridgeisasynonymforthe"handle/body"idiom.Thisisadesignmechanismthat
encapsulatesanimplementationclassinsideofaninterfaceclass.Theformeristhebody,
andthelatteristhehandle.Thehandleisviewedbytheuserastheactualclass,butthe
workisdoneinthebody."Thehandle/bodyclassidiommaybeusedtodecomposea
complexabstractionintosmaller,moremanageableclasses.Theidiommayreflectthe
sharingofasingleresourcebymultipleclassesthatcontrolaccesstoit(e.g.reference
counting)."

Structure
TheClientdoesntwanttodealwithplatformdependentdetails.TheBridgepattern
encapsulatesthiscomplexitybehindanabstraction"wrapper".
Bridgeemphasizesidentifyinganddecoupling"interface"abstractionfrom"implementation"
abstraction.

Example
TheBridgepatterndecouplesanabstractionfromitsimplementation,sothatthetwocanvary
independently.Ahouseholdswitchcontrollinglights,ceilingfans,etc.isanexampleofthe
Bridge.Thepurposeoftheswitchistoturnadeviceonoroff.Theactualswitchcanbe
implementedasapullchain,simpletwopositionswitch,oravarietyofdimmerswitches.

Checklist
1. Decideiftwoorthogonaldimensionsexistinthedomain.Theseindependentconcepts

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

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

Readnext
ThisarticleistakenfromourbookDesignPatternsExplainedSimply(/designpatterns
book).
Allofthedesignpatternsarecompiledthere.Thebookiswritteninclear,simplelanguage
thatmakesiteasytoreadandunderstand(justlikethisarticle).

You might also like