introduction to software technology 4. grasp...
TRANSCRIPT
![Page 1: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/1.jpg)
IntroductiontoSoftwareTechnology4.GRASPPatterns
KlausOstermann
EinführungindieSoftwaretechnik1
![Page 2: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/2.jpg)
GRASPPatterns
EinführungindieSoftwaretechnik2
} ObjectDesign:} “Afteridentifyingyourrequirementsandcreatingadomainmodel,thenaddmethodstothesoftwareclasses,anddefinethemessagingbetweentheobjectstofulfilltherequirements.”
} Buthow?} Whatmethodbelongswhere?} Howshouldtheobjectsinteract?} Thisisacritical,important,andnon-trivialtask
![Page 3: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/3.jpg)
GRASPPatterns
EinführungindieSoftwaretechnik3
} TheGRASPpatternsarealearningaidto} helponeunderstandessentialobjectdesign} applydesignreasoninginamethodical,rational,explainableway.
} Thisapproachtounderstandingandusingdesignprinciplesisbasedonpatternsofassigningresponsibilities
![Page 4: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/4.jpg)
GRASP-Responsibilities
EinführungindieSoftwaretechnik4
} Responsibilitiesarerelatedtotheobligationsofanobjectintermsofitsbehavior.
} Twotypesofresponsibilities:} knowing} doing
} Doingresponsibilitiesofanobjectinclude:} doingsomethingitself,suchascreatinganobjectordoinga
calculation} initiatingactioninotherobjects} controllingandcoordinatingactivitiesinotherobjects
} Knowingresponsibilitiesofanobjectinclude:} knowingaboutprivateencapsulateddata} knowingaboutrelatedobjects} knowingaboutthingsitcanderiveorcalculate
![Page 5: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/5.jpg)
Designpatternsinarchitecture} Apatternisarecurringsolutiontoastandardproblem,inacontext.
} ChristopherAlexander,professorofarchitecture…} Whyiswhataprofofarchitecturesaysrelevanttosoftware?
} “Apatterndescribesaproblemwhichoccursoverandoveragaininourenvironment,andthendescribesthecoreofthesolutiontothatproblem,insuchawaythatyoucanusethissolutionamilliontimesover,withouteverdoingitthesamewaytwice.”
![Page 6: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/6.jpg)
Patternsinengineering} Howdootherengineersfindandusepatterns?
} Matureengineeringdisciplineshavehandbooksdescribingsuccessfulsolutionstoknownproblems
} Automobiledesignersdon'tdesigncarsfromscratchusingthelawsofphysics
} Instead,theyreusestandarddesignswithsuccessfultrackrecords,learningfromexperience
} Shouldsoftwareengineersmakeuseofpatterns?Why?} Developingsoftwarefromscratchisalsoexpensive
} Patternsareaformofreuseofsoftwaredesign
![Page 7: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/7.jpg)
Definitionsandnames} Alexander:“Apatternisarecurringsolutiontoastandardproblem,inacontext.”
} Larman:“InOOdesign,apatternisanameddescriptionofaproblemandsolutionthatcanbeappliedinnewcontexts;ideally,apatternadvisesusonhowtoapplythesolutioninvaryingcircumstancesandconsiderstheforcesandtrade-offs.”
![Page 8: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/8.jpg)
BasicPatternPrinciples
EinführungindieSoftwaretechnik8
} Patternsareneverinvented;theyarefound} theycodifyexistingtried-and-trueknowledge,idioms,andprinciples
} themorehonedandwidelyused,thebetter
} Patternsgiveanametoanidea!
Fred:"WheredoyouthinkweshouldplacetheresponsibilityforcreatingaSalesLineltem?IthinkaFactory."Wilma:"ByCreator,IthinkSalewillbesuitable."Fred:"Oh,right-Iagree."
![Page 9: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/9.jpg)
GRASP} Namechosentosuggesttheimportanceofgraspingfundamentalprinciplestosuccessfullydesignobject-orientedsoftware
} AcronymforGeneralResponsibilityAssignmentSoftwarePatterns} (technically“GRASPPatterns”ishenceredundantbutitsoundsbetter)
} Describefundamentalprinciplesofobjectdesignandresponsibility
} Expressedaspatterns
![Page 10: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/10.jpg)
NineGRASPpatterns:} InformationExpert} Creator} LowCoupling} Controller} HighCohesion} Polymorphism} Indirection} PureFabrication} ProtectedVariations
![Page 11: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/11.jpg)
NineGRASPpatterns:} InformationExpert} Creator} LowCoupling} Controller} HighCohesion} Polymorphism} Indirection} PureFabrication} ProtectedVariations
![Page 12: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/12.jpg)
InformationExpert
EinführungindieSoftwaretechnik12
} Problem:Whatisageneralprincipleofassigningresponsibilitiestoobjects?
} Solution:Assignaresponsibilitytotheinformationexpert} theclassthathastheinformationnecessarytofulfilltheresponsibility
} Startassigningresponsibilitiesbyclearlystatingresponsibilities!
} Forinstance,inaPOSapplicationastatementmightbe:“Whoshouldberesponsibleforknowingthegrandtotalofasale"?
![Page 13: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/13.jpg)
InformationExpert
EinführungindieSoftwaretechnik13
} Whatinformationisneededtodeterminethegrandtotal?
} Saleistheinformationexpertforthisresponsibility.
![Page 14: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/14.jpg)
InformationExpert
EinführungindieSoftwaretechnik14
} Whatinformationisneededtodeterminethelineitemsubtotal?
![Page 15: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/15.jpg)
InformationExpert
EinführungindieSoftwaretechnik15
} Tofulfilltheresponsibilityofknowingandansweringitssubtotal,aSalesLineltemneedstoknowtheproductprice.
} TheProductSpecificationisaninformationexpertonansweringitsprice;therefore,amessagemustbesenttoitaskingforitsprice.
![Page 16: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/16.jpg)
InformationExpert
EinführungindieSoftwaretechnik16
} Tofulfilltheresponsibilityofknowingandansweringthesale'stotal,threeresponsibilitieswereassignedtothreedesignclassesofobjects
![Page 17: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/17.jpg)
InformationExpert:FinalDesign
EinführungindieSoftwaretechnik17
![Page 18: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/18.jpg)
InformationExpert:Discussion
EinführungindieSoftwaretechnik18
} Expertusuallyleadstodesignswhereasoftwareobjectdoesthoseoperationsthatarenormallydonetotheinanimatereal-worldthingitrepresents} asaledoesnottellyouitstotal;itisaninanimatething
} InOOdesign,allsoftwareobjectsare"alive"or"animated,"andtheycantakeonresponsibilitiesanddothings.
} Theydothingsrelatedtotheinformationtheyknow.
![Page 19: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/19.jpg)
InformationExpert:Discussion
EinführungindieSoftwaretechnik19
} Contraindication:Conflictwithseparationofconcerns} Example:Whoisresponsibleforsavingasaleinthedatabase?} AddingthisresponsibilitytoSalewoulddistributedatabaselogicovermanyclassesàlowcohesion
} Contraindication:Conflictwithlatebinding} Latebindingisavailableonlyforthereceiverobject} Butmaybethevariabilityoflatebindingisneededinsomemethodargumentinstead
} Example:Supportformultipleserializationstrategies
![Page 20: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/20.jpg)
NineGRASPpatterns:} InformationExpert} Creator} LowCoupling} Controller} HighCohesion} Polymorphism} Indirection} PureFabrication} ProtectedVariations
![Page 21: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/21.jpg)
Creator
21
Problem: Assignresponsibilityforcreatinganewinstanceofsomeclass?
Solution:Determinewhichclassshouldcreateinstancesofaclassbasedontherelationshipbetweenpotentialcreatorclassesandtheclasstobeinstantiated.
![Page 22: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/22.jpg)
Creator
22
} whohasresponsibilitytocreateanobject?} Bycreator,assignclassBresponsibilityofcreatinginstanceofclassAif} BaggregatesAobjects} BcontainsAobjects} BrecordsinstancesofAobjects} BcloselyusesAobjects} BhastheinitializingdataforcreatingAobjects
} wherethereisachoice,prefer} BaggregatesorcontainsAobjects
![Page 23: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/23.jpg)
Creator:Example
23
WhoisresponsibleforcreatingSalesLineItemobjects?LookforaclassthataggregatesorcontainsSalesLineItemobjects.
Sale
datetime
SalesLineItem
quantity
ProductSpecification
descriptionpriceUPC
Described-by
*
Contains
1..*
![Page 24: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/24.jpg)
Creator:Example
24
CreatorpatternsuggestsSale.Collaborationdiagramis
![Page 25: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/25.jpg)
Creator
25
} Promoteslowcouplingbymakinginstancesofaclassresponsibleforcreatingobjectstheyneedtoreference
} Bycreatingtheobjectsthemselves,theyavoidbeingdependentonanotherclasstocreatetheobjectforthem
![Page 26: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/26.jpg)
Creator:Discussion
EinführungindieSoftwaretechnik26
} Contraindications:} creationmayrequiresignificantcomplexity,suchas
} usingrecycledinstancesforperformancereasons} conditionallycreatinganinstancefromoneofafamilyofsimilarclassesbaseduponsomeexternalpropertyvalue
} Sometimesdesiredtooutsourceobjectwiring(“dependencyinjection”)
} Relatedpatterns:} AbstractFactory,Singleton,DependencyInjection
![Page 27: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/27.jpg)
NineGRASPpatterns:} InformationExpert} Creator} LowCoupling} Controller} HighCohesion} Polymorphism} Indirection} PureFabrication} ProtectedVariations
![Page 28: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/28.jpg)
LowCoupling
28
Problem:Howtosupportlowdependency,lowchangeimpact,andincreasedreuse.
Solution:Assignaresponsibilitysothatcouplingremainslow.
![Page 29: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/29.jpg)
WhyHighCouplingisundesirable
EinführungindieSoftwaretechnik29
} Couplingisameasureofhowstronglyoneelementisconnectedto,hasknowledgeof,orreliesonotherelements.
} Anelementwithlow(orweak)couplingisnotdependentontoomanyotherelements(classes,subsystems,…)} "toomany"iscontext-dependent
} Aclasswithhigh(orstrong)couplingreliesonmanyotherclasses.} Changesinrelatedclassesforcelocalchanges.} Suchclassesarehardertounderstandinisolation.} Theyarehardertoreusebecauseitsuserequirestheadditionalpresenceoftheclassesonwhichitisdependent.
![Page 30: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/30.jpg)
LowCoupling
30
Howcanwemakeclassesindependentofotherclasses?changesarelocalisedeasiertounderstandeasiertoreuseWhohasresponsibilitytocreateapaymentandassociateittoasale?
![Page 31: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/31.jpg)
LowCoupling
31
Twopossibilities:
1.Register
2.Sale
LowcouplingsuggestsSalebecauseSalehastobecoupledtoPaymentanyway(Saleknowsitstotal).
![Page 32: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/32.jpg)
CommonFormsofCouplinginOOLanguages
32
} TypeXhasanattribute(datamemberorinstancevariable)thatreferstoaTypeYinstance,orTypeYitself.
} TypeXhasamethodwhichreferencesaninstanceofTypeY,orTypeYitself,byanymeans.} TypicallyincludeaparameterorlocalvariableoftypeTypeY,orthe
objectreturnedfromamessagebeinganinstanceofTypeY.
} TypeXisadirectorindirectsubclassofTypeY.} TypeYisaninterface,andTypeXimplementsthatinterface.
![Page 33: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/33.jpg)
LowCoupling:Discussion
EinführungindieSoftwaretechnik33
} LowCouplingisaprincipletokeepinmindduringalldesigndecisions
} Itisanunderlyinggoaltocontinuallyconsider.} Itisanevaluativeprinciplethatadesignerapplieswhileevaluatingalldesigndecisions.
} LowCouplingsupportsthedesignofclassesthataremoreindependent} reducestheimpactofchange.
} Can'tbeconsideredinisolationfromotherpatternssuchasExpertandHighCohesion
} Needstobeincludedasoneofseveraldesignprinciplesthatinfluenceachoiceinassigningaresponsibility.
![Page 34: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/34.jpg)
LowCoupling:Discussion
EinführungindieSoftwaretechnik34
} Subclassingproducesaparticularlyproblematicformofhighcoupling} Dependenceonimplementationdetailsofsuperclass} “FragileBaseClassProblem”[seeSEDesignLecture]
} Extremelylowcouplingmayleadtoapoordesign} Fewincohesive,bloatedclassesdoallthework;allotherclassesarejustdatacontainers
} Contraindications:Highcouplingtoverystableelementsisusuallynotproblematic
![Page 35: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/35.jpg)
NineGRASPpatterns:} InformationExpert} Creator} LowCoupling} Controller} HighCohesion} Polymorphism} Indirection} PureFabrication} ProtectedVariations
![Page 36: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/36.jpg)
HighCohesion
36
Problem:Howtokeepcomplexitymanageable.
Solution:Assignresponsibilitiessothatcohesionremainshigh.
Cohesionisameasureofhowstronglyrelatedandfocusedtheresponsibilitiesofanelementare.
Anelementwithhighlyrelatedresponsibilities,andwhichdoesnotdoatremendousamountofwork,hashighcohesion
![Page 37: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/37.jpg)
Highcohesion
37
} Classesareeasiertomaintain} Easiertounderstand} Oftensupportlowcoupling} Supportsreusebecauseoffinegrainedresponsibility
![Page 38: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/38.jpg)
HighCohesion
38
Whohasresponsibilitytocreateapayment?
1.Register
looksOKifmakePayementconsideredinisolation,butaddingmoresystemoperations,Registerwouldtakeonmoreandmoreresponsibilitiesandbecomelesscohesive.
![Page 39: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/39.jpg)
HighCohesion
39
GivingresponsibilitytoSalesupportshighercohesioninRegister,aswellaslowcoupling.
![Page 40: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/40.jpg)
HighCohesion:Discussion
40
} Scenarios:} VeryLowCohesion:AClassissolelyresponsibleformanythingsinvery
differentfunctionalareas} LowCohesion:Aclasshassoleresponsibilityforacomplextaskinone
functionalarea.} HighCohesion.Aclasshasmoderateresponsibilitiesinonefunctionalarea
andcollaborateswithclassestofulfiltasks.} Advantages:
} Classesareeasiertomaintain} Easiertounderstand} Oftensupportlowcoupling} Supportsreusebecauseoffinegrainedresponsibility
} Ruleofthumb:aclasswithhighcohesionhasarelativelysmallnumberofmethods,withhighlyrelatedfunctionality,anddoesnotdotoomuchwork.
![Page 41: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/41.jpg)
Problem:HighCohesionandViewpoints
EinführungindieSoftwaretechnik41 [Harrison&Ossher‘93]
![Page 42: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/42.jpg)
NineGRASPpatterns:} InformationExpert} Creator} LowCoupling} Controller} HighCohesion} Polymorphism} Indirection} PureFabrication} ProtectedVariations
![Page 43: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/43.jpg)
Controller
43
Problem:Whoshouldberesponsibleforhandlinganinputsystemevent?
Solution:Assigntheresponsibilityforreceivingorhandlingasystemeventmessagetoaclassrepresentingtheoverallsystem,device,orsubsystem(facadecontroller)orausecasescenariowithinwhichthesystemeventoccurs(usecasecontroller)
![Page 44: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/44.jpg)
Controller:Example
EinführungindieSoftwaretechnik44
![Page 45: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/45.jpg)
Controller:Example
EinführungindieSoftwaretechnik45
} BytheControllerpattern,herearesomechoices:} Register,POSSystem:representstheoverall"system,"device,orsubsystem
} ProcessSaleSession,ProcessSaleHandler:representsareceiverorhandlerofallsystemeventsofausecasescenario
![Page 46: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/46.jpg)
Controller:Discussion
EinführungindieSoftwaretechnik46
} Normally,acontrollershoulddelegatetootherobjectstheworkthatneedstobedone;itcoordinatesorcontrolstheactivity.Itdoesnotdomuchworkitself.
} Facadecontrollersaresuitablewhentherearenot"toomany"systemevents
} Ausecasecontrollerisanalternativetoconsiderwhenplacingtheresponsibilitiesinafacadecontrollerleadstodesignswithlowcohesionorhighcoupling} typicallywhenthefacadecontrollerisbecoming"bloated"withexcessiveresponsibilities.
![Page 47: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/47.jpg)
Controller:Discussion
EinführungindieSoftwaretechnik47
} Benefits} Increasedpotentialforreuse,andpluggableinterfaces
} NoapplicationlogicintheGUI
} Dedicatedplacetoplacestatethatbelongstosomeusecase} E.g.operationsmustbeperformedinaspecificorder
} Avoidbloatedcontrollers!} E.g.singlecontrollerforthewholesystem,lowcohesion,lotsofstateincontroller
} Splitintousecasecontrollers,ifapplicable
} Interfacelayerdoesnothandlesystemevents
![Page 48: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/48.jpg)
NineGRASPpatterns:} InformationExpert} Creator} LowCoupling} Controller} HighCohesion} Polymorphism} Indirection} PureFabrication} ProtectedVariations
![Page 49: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/49.jpg)
Polymorphism
49
Problem:Howtohandlealternativesbasedontypes?
Howtocreatepluggablesoftwarecomponents?Solution: Whenalternatebehavioursareselectedbasedonthetypeofanobject,usepolymorphicmethodcalltoselectthebehaviour,ratherthanusingif/casestatementtotestthetype.
![Page 50: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/50.jpg)
Polymorphism:Example
EinführungindieSoftwaretechnik50
![Page 51: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/51.jpg)
Polymorphism:Discussion
51
} Polymorphismisafundamentalprincipleindesigninghowasystemisorganizedtohandlesimilarvariations.
} Properties:} Easierandmorereliablethanusingexplicitselectionlogic} Easiertoaddadditionalbehaviorslateron} Increasesthenumberclassesinadesign} Maymakethecodelesseasytofollow
} Usingtheprincipleexcessivelyfor“future-proofing”againstyetunknownpotentialfuturevariationsisabadidea} Agilemethodsrecommendtodonosignificant“upfrontdesign”andaddthevariationpointonlywhentheneedarises
![Page 52: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/52.jpg)
NineGRASPpatterns:} InformationExpert} Creator} LowCoupling} Controller} HighCohesion} Polymorphism} Indirection} PureFabrication} ProtectedVariations
![Page 53: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/53.jpg)
PureFabrication
53
Problem:Addingsomeresponsibilitiestodomainobjectswouldviolatehighcohesion/lowcoupling/reuse
Solution:Assignahighlycohesivesetofresponsibilitiestoanartificialorconvenienceclassthatdoesnotrepresentaproblemdomainconcept—somethingmadeup,tosupporthighcohesion,lowcoupling,andreuse.
![Page 54: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/54.jpg)
PureFabrication:Example
54
} In the point of sale example support is needed to save Saleinstancesinarelationaldatabase.
} By Expert, there is some justification to assign thisresponsibilitytoSaleclass.
} However, the task requires a relatively large number ofsupporting database-oriented operations and the Sale classbecomesincohesive.
} The sale class has to be coupled to the relational databaseincreasingitscoupling.
} Saving objects in a relational database is a very general taskfor which many classes need support. Placing theseresponsibilities in theSaleclass suggests there isgoing tobepoor reuseor lotsofduplication inotherclasses thatdo thesamething.
![Page 55: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/55.jpg)
PureFabrication:Example
55
} Solution:createanewclassthatissolelyresponsibleforsavingobjectsinapersistentstoragemedium
} ThisclassisaPureFabrication
} TheSaleremainswell-designed,withhighcohesionandlowcoupling
} ThePersistentStorageBrokerclassisitselfrelativelycohesive} ThePersistentStorageBrokerclassisaverygenericandreusable
object
![Page 56: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/56.jpg)
PureFabrication:Discussion
56
} Thedesignofobjectscanbebroadlydividedintotwogroups:} Thosechosenbyrepresentationaldecomposition(e.g.Sale)} Thosechosenbybehavioraldecomposition(e.g.analgorithmobjectsuchasTOCGeneratororPersistentStorage)
} Bothchoicesarevaliddesigns,althoughthesecondonecorrespondslesswelltothemodelingperspectiveonobjects
} Ifoverused,itcanleadtoanon-idiomaticdesign,namelyaseparationofthecodeintodataandbehaviorasinproceduralprogramming} CouplingofdataandbehavioriscentraltoOOdesign
![Page 57: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/57.jpg)
NineGRASPpatterns:} InformationExpert} Creator} LowCoupling} Controller} HighCohesion} Polymorphism} Indirection} PureFabrication} ProtectedVariations
![Page 58: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/58.jpg)
Indirection
58
Problem:Wheretoassignaresponsibility,toavoiddirectcouplingbetweentwo(ormore)things?Howtode-coupleobjectssothatlowcouplingissupportedandreusepotentialremainshigher?
Solution: Assigntheresponsibilitytoanintermediateobjecttomediatebetweenothercomponentsorservices,sothattheyarenotdirectlycoupled.
"Mostproblemsincomputersciencecanbesolved
byanotherlevelofindirection"
![Page 59: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/59.jpg)
Indirection:Example
EinführungindieSoftwaretechnik59
Byaddingalevelofindirectionandaddingpolymorphism,theadapterobjectsprotecttheinnerdesignagainstvariationsintheexternalinterfaces
![Page 60: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/60.jpg)
NineGRASPpatterns:} InformationExpert} Creator} LowCoupling} Controller} HighCohesion} Polymorphism} Indirection} PureFabrication} ProtectedVariations
![Page 61: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/61.jpg)
ProtectedVariation
EinführungindieSoftwaretechnik61
Problem:Howtodesignobjects,subsystems,andsystemssothatthevariationsorinstabilityintheseelementsdoesnothaveanundesirableimpactonotherelements?Solution:Identifypointsofpredictedvariationorinstability;assignresponsibilitiestocreateastableinterfacearoundthem.Note:Thisisbasicallyjustanotherformulationoftheinformationhidingprinciple.
![Page 62: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/62.jpg)
ProtectedVariation:Examples
EinführungindieSoftwaretechnik62
} Dataencapsulation,interfaces,polymorphism,indirection,andstandardsaremotivatedbyPV.
} VirtualmachinesarecomplexexamplesofindirectiontoachievePV
} Servicelookup:Clientsareprotectedfromvariationsinthelocationofservices,usingthestableinterfaceofthelookupservice.
} UniformAccessPrinciple} LawofDemeter} …
![Page 63: Introduction to Software Technology 4. GRASP Patternsps.informatik.uni-tuebingen.de/teaching/ss18/se/5_grasp.pdf · GRASP Patterns 3 Einführung in die Softwaretechnik } The GRASP](https://reader034.vdocuments.net/reader034/viewer/2022051512/6034b194a969b45bcc3051cd/html5/thumbnails/63.jpg)
Literature
EinführungindieSoftwaretechnik63
} CraigLarman,ApplyingUMLandPatterns,PrenticeHall,2004} Chapter16+17+22introduceGRASP