+ All Categories
Home > Documents > Introduction to Software Technology 3. Software...

Introduction to Software Technology 3. Software...

Date post: 19-Aug-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
60
Introduction to Software Technology 3. Software Design Klaus Ostermann Einführung in die Softwaretechnik 1
Transcript
Page 1: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

IntroductiontoSoftwareTechnology3.SoftwareDesign

KlausOstermann

EinführungindieSoftwaretechnik1

Page 2: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

GoalofSoftwareDesign

EinführungindieSoftwaretechnik2

}  Foreachdesiredprogrambehaviorthereareinfinitelymanyprogramsthathavethisbehavior}  Whatarethedifferencesbetweenthevariants?}  Whichvariantshouldwechoose?

}  Sinceweusuallyhavetosynthesizeratherthanchoosethesolution…}  Howcanwedesignavariantthathasthedesiredproperties?

Page 3: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Example

EinführungindieSoftwaretechnik3

}  Sortingwithconfigurableorder,variantA

void sort(int[] list, String order) { … boolean mustswap; if (order.equals(“up”)) { mustswap = list[i] < list[j]; } else if (order.equals(“down”)) { mustswap = list[i] > list[j]; } … }

Page 4: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Example

EinführungindieSoftwaretechnik4

}  Sortingwithconfigurableorder,variantBvoid sort(int[] list, Comparator cmp) { … boolean mustswap; mustswap = cmp.compare(list[i], list[j]); … } interface Comparator { boolean compare(int i, int j); } class UpComparator implements Comparator { boolean compare(int I, int j) { return i<j; }} class DownComparator implements Comparator { boolean compare(int I, int j) { return i>j; }}

(bytheway,thisdesigniscalled“strategypattern”)

Page 5: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

QualityofaSoftwareDesign

EinführungindieSoftwaretechnik5

}  Howcanwemeasuretheinternalqualityofasoftwaredesign?}  Extensibility,Maintainability,Understandability,Readability,…}  Robustnesstochange}  LowCoupling&HighCohesion}  Reusability}  Allthesequalitiesaretypicallysummarizedbythetermmodularity

}  …asopposedtoexternalquality}  Correctness:Validimplementationofrequirements}  EaseofUse}  Resourceconsumption}  Legalissues,politicalissues,…

Page 6: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Modularity

EinführungindieSoftwaretechnik6

Page 7: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Modularity

EinführungindieSoftwaretechnik7

}  Asoftwareconstructionmethodismodularifithelpsdesignerstoproducesoftwaresystemsmadeofautonomouselementsconnectedbyacoherent,simplestructure

}  Inthefollowingwe’llelaborateonthat:}  Fivecriteria}  FiveRules}  FivePrinciples

Page 8: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveCriteria:ModularDecomposability

EinführungindieSoftwaretechnik8

AsoftwareconstructionmethodsatisfiesModularDecomposabilityifithelpsinthetaskofdecomposingasoftwareproblemintoasmallnumberoflesscomplexsubproblems,connectedbyasimplestructure,and

independentenoughtoallowfurtherworktoproceedseparatelyoneachofthem.

Page 9: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveCriteria:ModularDecomposability

EinführungindieSoftwaretechnik9

}  ModularDecomposabilityimplies:DivisionofLaborpossible!

}  Example:Top-DownDesign}  Counter-Example:Productionofaglobalinitializationmodule

Page 10: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveCriteria:ModularComposability

EinführungindieSoftwaretechnik10

AmethodsatisfiesModularComposabilityifitfavorstheproductsofsoftwareelementswhichmaythenbefreelycombinedwitheachothertoproducenewsystems,

possiblyinanenvironmentquitedifferentfromtheoneinwhichtheywereinitiallydeveloped.

Page 11: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveCriteria:ModularComposability

EinführungindieSoftwaretechnik11

}  Isdualtomodulardecomposability}  Isdirectlyconnectedwithreusability

}  Old“dream”ofprogramming:programmingasconstructionboxactivity

}  Example1:Librarieshavebeenreusedsuccessfullyincountlessdomains

}  Example2:UnixShellCommands}  Counter-Example:Preprocessors

Page 12: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveCriteria:ModularUnderstandability

EinführungindieSoftwaretechnik12

AmethodfavorsModularUnderstandabilityifithelpsproducesoftwareinwhichahumanreadercanunderstandeachmodulewithouthavingtoknowtheothers,or,atworst,byhavingtoexamineonlyafewoftheothers.

Page 13: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveCriteria:ModularUnderstandability

EinführungindieSoftwaretechnik13

}  Importantformaintenance}  Appliestoallsoftwareartifacts,notjustcode}  Counter-example:Sequentialdependenciesbetweenmodules

Page 14: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveCriteria:ModularContinuity

EinführungindieSoftwaretechnik14

AmethodsatisfiesModularContinuityif,inthesoftwarearchitecturesthatityields,asmallchangeintheproblemspecificationwilltriggerachangeofjustonemodule,ora

smallnumberofmodules.

Page 15: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveCriteria:ModularContinuity

EinführungindieSoftwaretechnik15

}  Example1:Symbolicconstants(asopposedtomagicnumbers)

}  Example2:Hidingdatarepresentationbehindaninterface

}  Counter-Example:Programdesignsdependingonfragiledetailsofhardwareorcompiler

Page 16: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveCriteria:ModularProtection

EinführungindieSoftwaretechnik16

AmethodsatisfiedModularProtectionifityieldsarchitecturesinwhichtheeffectofanabnormal

conditionoccurringatruntimeinamodulewillremainconfinedtothatmodule,oratworstwillonlypropagate

toafewneighboringmodules.

Page 17: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveCriteria:ModularProtection

EinführungindieSoftwaretechnik17

}  Motivation:Bigsoftwarewillalwayscontainbugsetc.,failuresunavoidable

}  Example:DefensiveProgramming}  Counter-Example:Anerroneousnullpointerinonemoduleleadstoanerrorinadifferentmodule

Page 18: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveRules

EinführungindieSoftwaretechnik18

}  FiveRuleswillfollowwhichwemustobservetoensurehigh-qualitydesign

Page 19: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveRules:DirectMapping

EinführungindieSoftwaretechnik19

Themodularstructuredevisedintheprocessofbuildingasoftwaresystemshouldremaincompatiblewithanymodularstructuredevisedintheprocessof

modelingtheproblemdomain.

Page 20: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveRules:DirectMapping

EinführungindieSoftwaretechnik20

}  Followsfromcontinuityanddecomposability}  A.k.a.“lowrepresentationalgap”[C.Larman]

Page 21: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveRules:FewInterfaces

EinführungindieSoftwaretechnik21

Iftwomodulescommunicate,theyshouldexchangeaslittleinformationaspossible

Page 22: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveRules:FewInterfaces

EinführungindieSoftwaretechnik22

}  Wanttopologywithfewconnections}  Followsfromcontinuityandprotection;otherwisechanges/errorswouldpropagatemore

Page 23: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveRules:SmallInterfaces

EinführungindieSoftwaretechnik23

Iftwomodulescommunicate,theyshouldexchangeaslittleinformationaspossible

Page 24: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveRules:SmallInterfaces

EinführungindieSoftwaretechnik24

}  Followsfromcontinuityandprotection,requiredforcomposability

}  Counter-Example:BigInterfacesJ

Page 25: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveRules:ExplicitInterfaces

EinführungindieSoftwaretechnik25

WhenevertwomodulesAandBcommunicate,thismustbeobviousfromtheinterfaceofAorBorboth.

Page 26: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveRules:ExplicitInterfaces

EinführungindieSoftwaretechnik26

}  Counter-Example1:GlobalVariables}  Counter-Example2:Aliasing–mutationofsharedheapstructures

Page 27: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Intermezzo:LawofDemeter(LoD)

EinführungindieSoftwaretechnik27

}  LoD:Eachmoduleshouldhaveonlylimitedknowledgeaboutotherunits:onlyunits"closely"relatedtothecurrentunit

}  Inparticular:Don’ttalktostrangers!}  Forinstance,noa.getB().getC().foo()}  Motivatedbycontinuity

Page 28: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveRules:InformationHiding

EinführungindieSoftwaretechnik28

Thedesignerofeverymodulemustselectasubsetofthemodule’spropertiesastheofficialinformationaboutthemodule,tobemadeavailabletoauthorsofclientmodules.

Page 29: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveRules:InformationHiding

EinführungindieSoftwaretechnik29

Page 30: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FiveRules:InformationHiding

EinführungindieSoftwaretechnik30

}  Reynolds’parableaboutcomplexnumbers…}  Impliedbycontinuity}  Theiceberganalogyisslightlymisleading,sinceaninterfacealsoabstractsovertheimplementation

Page 31: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples

EinführungindieSoftwaretechnik31

}  Fromtheprecedingrules,andindirectlyfromthecriteria,fiveprinciplesofsoftwareconstructionfollow:}  TheLinguisticModularUnitsprinciple.}  TheSelf-Documentationprinciple.}  TheUniformAccessprinciple.}  TheOpen-Closedprinciple.}  TheSingleChoiceprinciple.

Page 32: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples:LinguisticModularUnits

EinführungindieSoftwaretechnik32

Modulesmustcorrespondtosyntacticunitsinthelanguageused.

Page 33: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples:LinguisticModularUnits

EinführungindieSoftwaretechnik33

}  Excludesmethodsthatsuggestacertainmoduleconceptandalanguagethatdoesnotofferthecorrespondingmodularconstruct

}  Impliedbycontinuityanddirectmapping}  Bothrequiredirectcorrespondencebetweenspecification,design,andimplementationmodules

}  Impliedbydecomposabilityandcomposability}  Theimplementationofeverytaskmustresultinawell-delimitedsyntacticunit

Page 34: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples:Self-DocumentationPrinciple

EinführungindieSoftwaretechnik34

Thedesignerofamoduleshouldstrivetomakeallinformationaboutthe

modulepartofthemoduleitself.

Page 35: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples:Self-DocumentationPrinciple

EinführungindieSoftwaretechnik35

}  Precludeskeepinginformationaboutthemoduleinaseparatedocument

}  Justification:}  Modularunderstandabilityprinciple}  Continuity,hardtokeepseparatedocuments“insync”}  Ingeneral:Changeability

}  Traditional“heavy-weight”SEprocesseshaveadifferentpointofviewonthis

}  Seealsomaterialofpreviouslectureaboutliterateprogrammingandgooddocumentationingeneral

Page 36: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples:UniformAccess

EinführungindieSoftwaretechnik36

Allservicesofferedbyamoduleshouldbeavailablethroughauniform

notation,whichdoesnotbetraywhethertheyareimplementedthrough

storageorthroughcomputation

Page 37: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples:UniformAccess

EinführungindieSoftwaretechnik37

}  Justification:Continuitycriterion,specialcaseofinformationhiding

}  Example:Thebalanceofanaccountmaybestoredasdata,oritmaybecomputedfromthelistoftransactions}  Thisisatime/spacetradeoff}  Differenceshouldnotbevisibleforaclient

}  Somelanguagessupportthisprincipledirectly}  Ruby,Eiffel,Python(*),Smalltalk(*)}  Adesignconventioninotherlanguages

}  “getter/setter”methodsinJava

Page 38: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples:Open-ClosedPrinciple

EinführungindieSoftwaretechnik38

Modulesshouldbebothopenandclosed.

Page 39: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples:Open-ClosedPrinciple

EinführungindieSoftwaretechnik39

}  Amoduleissaidtobeopenifitisstillavailableforextension.}  Forexample,itshouldbepossibletoexpanditssetofoperationsoraddfieldstoitsdatastructures.

}  Amoduleissaidtobeclosedifitisavailableforusebyothermodules.}  Well-definedstableinterface}  Canbecompiled,stored,…

}  Motivation:Opennessforfutureextensions,closednessforcomposition

Page 40: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples:Open-ClosedPrinciple

EinführungindieSoftwaretechnik40

}  Example:ClassesinOOlanguagesareopenthroughinheritanceyetcanbeusedthroughconstructorcalls

}  Counter-Example:PackagesinJava}  Whathappensifmodulesarenotopen:

}  “Monkeypatching”inJavascript

(youdon’tneedtounderstandthisexampleindetail)

eval("getBrowser().removeTab ="+ getBrowser().removeTab.toString().replace( 'this.addTab("about:blank");', 'if (SpeedDial.loadInLastTab) {this.addTab(' +'"chrome://speeddial/content/speeddial.xul"' +')} else { this.addTab("about:blank")}' ));

Page 41: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples:SingleChoice

EinführungindieSoftwaretechnik41

Wheneverasoftwaresystemmustsupportasetofalternatives,oneandonlyonemoduleinthesystemshouldknowtheirexhaustivelist.

Page 42: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples:SingleChoice

EinführungindieSoftwaretechnik42

}  SpecialcaseoftheDRYprinciple(Don’trepeatyourself):}  Everypieceofknowledgemusthaveasingle,unambiguous,authoritativerepresentationwithinasystem

}  Typicalexamplesofviolationsofthisprinciple:}  MultipleIf/then/elseorcasestatementswithidenticalconditions

}  PatternMatchinginfunctionalprogramming

}  Isaconsequenceofopen-closedprinciple}  Isaformofinformationhiding

Page 43: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

FivePrinciples:SingleChoice

EinführungindieSoftwaretechnik43

}  AvoidedinOOlanguagesbyusingsubtypingandlatebinding}  Cf.sortingexampleinthebeginningofthispresentation}  However,OOviolatestheprincipleitselfinthatthelistofmethodsinaninterfaceisclosedandreplicatedinallimplementations

}  Simpleprinciplebutquitehardtorealize}  Cf.solutionstotheso-called‘expressionproblem’

Page 44: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Discussion

EinführungindieSoftwaretechnik44

}  Examinethemodularstructuresofanyprogramminglanguagewhichyouknow

}  Assesshowtheysupportthecriteriaandprinciplespresentedinthislecture

Page 45: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Reusability

EinführungindieSoftwaretechnik45

Page 46: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Reusability

EinführungindieSoftwaretechnik46

}  Olddreamof“mass-producedsoftwarecomponents”byMcIlroyatNATOconference}  Idea:Mimicengineering}  However,cf.pitfallsofconfusingproductandplan,seefirstlecture

}  Other(equallyproblematic)commonanalogy:SoftwareasLEGO

}  Althoughthesevisionsarebothproblematic,reuseisamajorconcerninsoftwaredesign

Page 47: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

GoalsofReuse

EinführungindieSoftwaretechnik47

}  Timeliness:Havinglesssoftwaretodevelopmeansthatwecanbuilditfaster

}  Decreasedmaintenanceeffort:Ifsomeoneelseisresponsibleforthesoftwareheorsheisalsoresponsibleforitsevolution}  Andhedoessoonceforallclients:Fixonce,profitmanytimes

}  Redundancyisasourceofinconsistency.Reusecanavoidredundancy.

}  Reliability}  Ifsoftwareisreusedmanytimesmoreeffortisputintosoftware

quality}  Efficiency

}  Bythesameargumentsasreliability}  Consistency}  Investment

Page 48: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Whatshouldwereuse?

EinführungindieSoftwaretechnik48

}  Reuseofpersonnel}  E.g.avoidlossofknow-howbytransferringsoftwareengineersfromprojecttoproject

}  Reuseofdesignsandspecifications}  Notionofdesign/specificationassoftwareproductindependentofimplementationisdubious,cf.self-documentationprinciple

}  Theimplementationisthe(detailed)design}  Withcontinuity,directmappingetc.,thedistinctionbetweenreusingmodulesandreusingdesignstendstofadeaway}  High-LevelDesign(e.g.classdiagram)islikeatableofcontentsofthedetaileddesign(implementation)

Page 49: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Whatshouldwereuse?

EinführungindieSoftwaretechnik49

}  DesignPatterns}  =architecturalideasapplicableacrossabroadrangeofapplicationdomains

}  Animportantformofarchitecturereusewhichwewillstudyindetaillateron

}  SourceCode}  Wewanttoreusesomethingthat“runs”,sinceeventuallywewanttorunprograms(“Bubblesdon’tcrash!”)

Page 50: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Reuseofabstractedmodules

EinführungindieSoftwaretechnik50

}  Ifwewanttoreusesourcecode,inwhatformshouldwereuseit?

}  Reusingitintheformoftheactualsourcetextisproblematic}  Itremovesinformationhiding:Usersmayrelyonimplementationdetailsonlyvisiblebystudyingthesourcetext

}  Developersofsoftwaredistributedinsourcetextmaybetemptedtoviolatemodularityrules

}  Whatwewanttoreuseareabstractedmodules}  Softwaredescribedthroughawell-definedinterfacethathidesimplementationdetails

}  Thesoftwaremaystillbedistributedinsourcetext;thedifferenceistheprimarysourceofinformationaboutit

Page 51: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

ReuseandPatterns

EinführungindieSoftwaretechnik51

}  Areusablepieceofcodeabstractsovercertainpatterns(patternsinthegeneralsense,notdesignpatterns)

}  Conversely,aprogramcontainsopportunitiesforreuseifitcontainspatterns}  Canbeformalizedas:TheKolmogorovcomplexityoftheprogramissmallerthantheprogram

}  Let’slookatsometypicalpatternsofredundancy(patternsofpatternsJ)andwaystoabstractoverthem

Page 52: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Reuse:Constants

EinführungindieSoftwaretechnik52

}  Sometimesthesamenumber/string/…showsupmanytimesinaprogram(“magicnumber”)}  Asourceofinconsistency,ifthenumber/stringmaychange

}  E.g.,thevalueofPiisunlikelytochangeJ

}  Nodocumentationoftheintentionofthenumber/string/…}  Reusebydefiningaconstant

}  Canreusetheconstantinallplaceswhereitisneeded}  Canchangeinoneplace}  Cangivemeaningfulnametoconstant

Page 53: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Reuse:CommonSubexpressions

EinführungindieSoftwaretechnik53

}  Codemaycontainthesamesubexpressionsinmanyplaces,e.g.,5*89+3orx+y/2 }  Mustkeeptrackofscopingrulesifvariablesareinvolved

}  Canabstractoverpatternbyusingalocalvariable}  Ifthesubexpressionperformsside-effects(I/O,mutation,…)canabstractoverpatternusingprocedure/method

}  Thismayormaynotyieldmoreefficientcode}  Dependingonhowsmartthecompileris

}  Moreimportantly,itwilltypicallyimprovethecode}  Givestheideabehindthecommonsubexpressionaname}  Lessredundancy,…

Page 54: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Reuse:Almostcommonsubexpressions

EinführungindieSoftwaretechnik54

}  Codecontainssimilar,butnotidenticalsubexpressions}  Differonlyinthevalueof“first-class”expressions

}  Canabstractoverpatternusingproceduralabstraction(or,methods,functions,lambdas,…)

}  E.g.average(x,y)=x+y/2insteadofcomputingaveragesinplace

}  Givesabstractionaname,avoidsredundancy,…

Page 55: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Reuse:Almostcommonsubexpressions

EinführungindieSoftwaretechnik55

}  Whatiftheexpressionsdifferinthesubroutinestheycall?}  Nothingchangesinalanguagewithfirst-classsubroutines;canabstractoverthesecallsandturnthemintoparameters}  E.g.functionallanguages

}  InOOlanguages,thisproblemhasgivenrisetodesignpatternssuchas“strategy”and“templatemethod”whichwewilldiscussindetaillateron

Page 56: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Reuse:Almostcommonsubexpressions

EinführungindieSoftwaretechnik56

}  Whatiftheexpressionsdifferinthetypestheyuse?}  E.g.quicksortalgorithmonintegersvs.quicksortonstrings

}  Canusegenerictypes!}  List<T>sort<T>(List<T>in,Comparator<T>cmp){…}

Page 57: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Reuse:Similarclassdefinitions

EinführungindieSoftwaretechnik57

}  Canfactoroutcommonalitiesincommonsuperclass}  Latebindingandpossibilitytoextendfields/methodsallowonetospecializeinpowerfulways}  However,subtypinglimitsthewayshowasubclasscanbedifferentfromasuperclass}  “LiskovSubstitutionPrinciple”àSEDesignLecture

}  However,differentpointsofviewonwhentouseinheritance}  “Scandinavian”styleofinheritance:Mechanismforconceptualspecialization,drivenbymodeling

}  “American”styleofinheritance:Reuseandpatchcode

Page 58: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Somepatternsarehardtoabstractover,though

EinführungindieSoftwaretechnik58

Page 59: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

ReuseandEfficiency

EinführungindieSoftwaretechnik59

}  Abstractdesignscanbelessefficient(intermsofspace/timebehavior)thantheirredundantexpansions}  “Alldesignisjustaddingmorelevelsofindirection”}  Aconflictbetweenreuseandefficiency?

}  Mostofthetimenotarealconcern}  Compilertechniquesremovemuchabstractionoverhead

}  Inlining,devirtualization,tail-calloptimization,partialevaluation,staging,…

}  Costofmethod/functioncallingrarelythemainbottleneck}  Onlyafractionofthecodeisperformance-criticalanyway

}  Canbeaconcernifveryinefficientabstractiontechniquesareused}  E.g.,reflection

Page 60: Introduction to Software Technology 3. Software …ps.informatik.uni-tuebingen.de/teaching/ss18/se/3_Design.pdfFive Principles: Open-Closed Principle 39 Einführung in die Softwaretechnik

Literature

EinführungindieSoftwaretechnik60

}  BertrandMeyer,Object-OrientedSoftwareConstruction,PrenticeHall,1997[Chapter3,4]


Recommended