You are on page 1of 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