(/) / 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).