introduction to software technology 4. grasp...

63
Introduction to Software Technology 4. GRASP Patterns Klaus Ostermann Einführung in die Softwaretechnik 1

Upload: others

Post on 07-Oct-2020

6 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Literature

EinführungindieSoftwaretechnik63

}  CraigLarman,ApplyingUMLandPatterns,PrenticeHall,2004}  Chapter16+17+22introduceGRASP