josh bloch charlie garrodcharlie/courses/17-214/2020... · 2020-04-30 · josh bloch charlie garrod...
TRANSCRIPT
![Page 1: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/1.jpg)
1 17-214
PrinciplesofSoftwareConstruction: Objects,Design,andConcurrencyPart1:DesigningclassesAformaldesignprocess:DomainmodelingJoshBloch CharlieGarrod
![Page 2: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/2.jpg)
2 17-214
Administrivia
• Homework3dueSundayat11:59p.m.• Optionalreadingfortoday:
• UMLandPatternsChapter17• EffectiveJavaitems49,54,and69
– RequiredreadingdueTuesday:• UMLandPatternsChapters14,15,and16
• MidtermexamnextThursday(February13th)– ReviewsessionWednesday,February12th6-8pm,DHA302– Practiceexamcomingthisweekend
![Page 3: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/3.jpg)
3 17-214
KeyconceptsfromTuesday
![Page 4: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/4.jpg)
4 17-214
KeyconceptsfromTuesday
• Moredesignpatternsforreuse– Templatemethodpattern– Iteratorpattern– Decoratorpattern
• Designprinciples:heuristicstoachievedesigngoals
![Page 5: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/5.jpg)
5 17-214
Adesignprincipleforreuse:lowcoupling
• Eachcomponentshoulddependonasfewothercomponentsaspossible
• Benefitsoflowcoupling:– Enhancesunderstandability– Reducescostofchange– Easesreuse
![Page 6: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/6.jpg)
6 17-214
Representationalgap
• Real-worldconcepts:
• Softwareconcepts:
PineTreeage height
harvest()
Forest -trees
…
Ranger …
surveyForest(…)
![Page 7: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/7.jpg)
7 17-214
Arelateddesignprinciple:highcohesion
• Eachcomponentshouldhaveasmallsetofclosely-relatedresponsibilities
• Benefits:– Facilitatesunderstandability– Facilitatesreuse– Easesmaintenance
PineTreeage height
harvest()
Forest -trees
…
Ranger …
surveyForest(…)
![Page 8: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/8.jpg)
8 17-214
Problem
Space
Domain Model
Solution Space
Object Model
Our path toward a more formal design process
• Real-worldconcepts• Requirements,concepts• Relationshipsamongconcepts• Solvingaproblem• Buildingavocabulary
• Systemimplementation• Classes,objects• Referencesamongobjectsand
inheritancehierarchies• Computingaresult• Findingasolution
![Page 9: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/9.jpg)
9 17-214
Today
• Interactiondiagrams:tovisualizedynamicbehavior• Understandingadesignproblem:Object-orientedanalysis
![Page 10: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/10.jpg)
10 17-214
Visualizingdynamicbehavior:Interactiondiagrams
• Aninteractiondiagramisapicturethatshows,forasinglescenarioofuse,theeventsthatoccuracrossthesystem’sboundaryorbetweensubsystems
• Clarifiesinteractions:– Betweentheprogramanditsenvironment– Betweenmajorpartsoftheprogram
• Forthiscourse,youshouldknowUMLsequencediagrams
![Page 11: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/11.jpg)
11 17-214
Constructingasequencediagram
![Page 12: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/12.jpg)
12 17-214
Anexamplesequencediagram
![Page 13: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/13.jpg)
13 17-214
(Sequencediagramwithnotationannotations)
![Page 14: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/14.jpg)
14 17-214
DrawasequencediagramforacalltoLoggingList.add:
publicclassLoggingList<E>implementsList<E>{privatefinalList<E>list;publicLoggingList<E>(List<E>list){this.list=list;}publicbooleanadd(Ee){System.out.println("Adding"+e);returnlist.add(e);}publicEremove(intindex){System.out.println("Removingat"+index);returnlist.remove(index);}…
![Page 15: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/15.jpg)
15 17-214
Today
• Interactiondiagrams:tovisualizedynamicbehavior• Understandingadesignproblem:Object-orientedanalysis
![Page 16: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/16.jpg)
16 17-214
Ahigh-levelsoftwaredesignprocess
• Projectinception• Gatherrequirements• Defineactors,andusecases• Model/diagramtheproblem,defineobjects• Definesystembehaviors• Assignobjectresponsibilities• Defineobjectinteractions• Model/diagramapotentialsolution• Implementandtestthesolution• Maintenance,evolution,…
17-313
17-214
…
![Page 17: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/17.jpg)
17 17-214
Artifactsofthisdesignprocess
• Model/diagramtheproblem,defineobjects– Domainmodel(a.k.a.conceptualmodel)
• Definesystembehaviors– Systemsequencediagram– Systembehavioralcontracts
• Assignobjectresponsibilities,defineinteractions– Objectinteractiondiagrams
• Model/diagramapotentialsolution– Objectmodel
![Page 18: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/18.jpg)
18 17-214
Artifactsofthisdesignprocess
• Model/diagramtheproblem,defineobjects– Domainmodel(a.k.a.conceptualmodel)
• Definesystembehaviors– Systemsequencediagram– Systembehavioralcontracts
• Assignobjectresponsibilities,defineinteractions– Objectinteractiondiagrams
• Model/diagramapotentialsolution– Objectmodel
Today:understandingtheproblem
Definingasolution
![Page 19: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/19.jpg)
19 17-214
Inputtothedesignprocess:Requirementsandusecases
• Typicallyprose:
![Page 20: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/20.jpg)
20 17-214
Modelingaproblemdomain
• Identifykeyconceptsofthedomaindescription– Identifynouns,verbs,andrelationshipsbetweenconcepts– Avoidnon-specificvocabulary,e.g."system"– Distinguishoperationsandconcepts– Brainstormwithadomainexpert
![Page 21: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/21.jpg)
21 17-214
Modelingaproblemdomain
• Identifykeyconceptsofthedomaindescription– Identifynouns,verbs,andrelationshipsbetweenconcepts– Avoidnon-specificvocabulary,e.g."system"– Distinguishoperationsandconcepts– Brainstormwithadomainexpert
• VisualizeasaUMLclassdiagram,adomainmodel– Showclassandattributeconcepts
• Real-worldconceptsonly• Nooperations/methods• Distinguishclassconceptsfromattributeconcepts
– Showrelationshipsandcardinalities
![Page 22: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/22.jpg)
22 17-214
Buildingadomainmodelforalibrarysystem
Apubliclibrarytypicallystoresacollectionofbooks,movies,orotherlibraryitemsavailabletobeborrowedbypeoplelivinginacommunity.Eachlibrarymembertypicallyhasalibraryaccountandalibrarycardwiththeaccount’sIDnumber,whichshecanusetoidentifyherselftothelibrary.Amember’slibraryaccountrecordswhichitemsthememberhasborrowedandtheduedateforeachborroweditem.Eachtypeofitemhasadefaultrentalperiod,whichdeterminestheitem’sduedatewhentheitemisborrowed.Ifamemberreturnsanitemaftertheitem’sduedate,thememberowesalatefeespecificforthatitem,anamountofmoneyrecordedinthemember’slibraryaccount.
![Page 23: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/23.jpg)
23 17-214
Buildingadomainmodelforalibrarysystem
Apubliclibrarytypicallystoresacollectionofbooks,movies,orotherlibraryitemsavailabletobeborrowedbypeoplelivinginacommunity.Eachlibrarymembertypicallyhasalibraryaccountandalibrarycardwiththeaccount’sIDnumber,whichshecanusetoidentifyherselftothelibrary.Amember’slibraryaccountrecordswhichitemsthememberhasborrowedandtheduedateforeachborroweditem.Eachtypeofitemhasadefaultrentalperiod,whichdeterminestheitem’sduedatewhentheitemisborrowed.Ifamemberreturnsanitemaftertheitem’sduedate,thememberowesalatefeespecificforthatitem,anamountofmoneyrecordedinthemember’slibraryaccount.
![Page 24: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/24.jpg)
24 17-214
Onedomainmodelforthelibrarysystem
![Page 25: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/25.jpg)
25 17-214
Notesonthelibrarydomainmodel
• Allconceptsareaccessibletoanon-programmer• TheUMLissomewhatinformal
– Relationshipsareoftendescribedwithwords• Real-world"is-a"relationshipsareappropriateforadomainmodel• Real-wordabstractionsareappropriateforadomainmodel• Iterationisimportant
– Thisexampleisafirstdraft.Someterms(e.g.Itemvs.LibraryItem,Accountvs.LibraryAccount)wouldlikelyberevisedinarealdesign.
• Aggregatetypesareusuallymodeledasclasses• Primitivetypes(numbers,strings)areusuallymodeledasattributes
![Page 26: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/26.jpg)
26 17-214
BuildadomainmodelforHomework2
![Page 27: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/27.jpg)
27 17-214
PossibledomainmodelsforHomework2
![Page 28: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/28.jpg)
28 17-214
Understandingsystembehaviorwithsequencediagrams
• Asystemsequencediagramisamodelthatshows,foronescenarioofuse,thesequenceofeventsthatoccuronthesystem’sboundary
• Designgoal:Identifyanddefinetheinterfaceofthesystem– System-levelcomponentsonly:e.g.,Auserandtheoverallsystem
![Page 29: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/29.jpg)
29 17-214
Understandingsystembehaviorwithsequencediagrams
• Asystemsequencediagramisamodelthatshows,foronescenarioofuse,thesequenceofeventsthatoccuronthesystem’sboundary
• Designgoal:Identifyanddefinetheinterfaceofthesystem– System-levelcomponentsonly:e.g.,Auserandtheoverallsystem
• Input:Domaindescriptionandoneusecase• Output:Asequencediagramofsystem-leveloperations
– Includeonlydomain-levelconceptsandoperations
![Page 30: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/30.jpg)
30 17-214
Onesequencediagramforthelibrarysystem
Usecasescenario:Alibrarymembershouldbeabletouseherlibrarycardtologinatalibrarysystemkioskandborrowabook.Afterconfirmingthatthememberhasnounpaidlatefees,thelibrarysystemshoulddeterminethebook’sduedatebyaddingitsrentalperiodtothecurrentday,andrecordthebookanditsduedateasaborrowediteminthemember’slibraryaccount.
![Page 31: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/31.jpg)
31 17-214
Formalizesystembehaviorwithbehavioralcontracts
• Asystembehavioralcontractdescribesthepre-conditionsandpost-conditionsforsomeoperationidentifiedinthesystemsequencediagrams– System-leveltextualspecifications,likesoftwarespecifications
![Page 32: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/32.jpg)
32 17-214
Asystembehavioralcontractforthelibrarysystem
Operation: borrow(item)Pre-conditions: Librarymemberhasalreadyloggedintothesystem.
Itemisnotcurrentlyborrowedbyanothermember.Post-conditions: Logged-inmember'saccountrecordsthenewly-borrowed
item,orthememberiswarnedshehasanoutstandinglatefee. Thenewly-borroweditemcontainsafutureduedate, computedastheitem'srentalperiodplusthecurrentdate.
![Page 33: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/33.jpg)
33 17-214
Distinguishingdomainvs.implementationconcepts
![Page 34: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/34.jpg)
34 17-214
Distinguishingdomainvs.implementationconcepts
• Domain-levelconcepts:– Almostanythingwithareal-worldanalogue
• Implementation-levelconcepts:– Implementation-likemethodnames– Programmingtypes– Visibilitymodifiers– Helpermethodsorclasses– Artifactsofdesignpatterns
![Page 35: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020... · 2020-04-30 · Josh Bloch Charlie Garrod 17-214 2 ... – Avoid non-specific vocabulary, e.g. "system" – Distinguish operations](https://reader033.vdocuments.net/reader033/viewer/2022050110/5f4803c0b269c373cd24ee43/html5/thumbnails/35.jpg)
35 17-214
Summary:Understandingtheproblemdomain
• Knowyourtoolstobuilddomain-levelrepresentations– Domainmodels– Systemsequencediagrams– Systembehavioralcontracts
• Befastand(sometimes)loose– Elideobvious(?)details– Iterate,iterate,iterate,…
• Getfeedbackfromdomainexperts– Useonlydomain-levelconcepts