josh bloch charlie garrodcharlie/courses/17-214/2020... · 2020-04-30 · josh bloch charlie garrod...

Post on 14-Jul-2020

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1 17-214

PrinciplesofSoftwareConstruction: Objects,Design,andConcurrencyPart1:DesigningclassesAformaldesignprocess:DomainmodelingJoshBloch CharlieGarrod

2 17-214

Administrivia

•  Homework3dueSundayat11:59p.m.•  Optionalreadingfortoday:

•  UMLandPatternsChapter17•  EffectiveJavaitems49,54,and69

–  RequiredreadingdueTuesday:•  UMLandPatternsChapters14,15,and16

•  MidtermexamnextThursday(February13th)–  ReviewsessionWednesday,February12th6-8pm,DHA302–  Practiceexamcomingthisweekend

3 17-214

KeyconceptsfromTuesday

4 17-214

KeyconceptsfromTuesday

•  Moredesignpatternsforreuse–  Templatemethodpattern–  Iteratorpattern–  Decoratorpattern

•  Designprinciples:heuristicstoachievedesigngoals

5 17-214

Adesignprincipleforreuse:lowcoupling

•  Eachcomponentshoulddependonasfewothercomponentsaspossible

•  Benefitsoflowcoupling:–  Enhancesunderstandability–  Reducescostofchange–  Easesreuse

6 17-214

Representationalgap

•  Real-worldconcepts:

•  Softwareconcepts:

PineTreeage height

harvest()

Forest -trees

Ranger …

surveyForest(…)

7 17-214

Arelateddesignprinciple:highcohesion

•  Eachcomponentshouldhaveasmallsetofclosely-relatedresponsibilities

•  Benefits:–  Facilitatesunderstandability–  Facilitatesreuse–  Easesmaintenance

PineTreeage height

harvest()

Forest -trees

Ranger …

surveyForest(…)

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

9 17-214

Today

•  Interactiondiagrams:tovisualizedynamicbehavior•  Understandingadesignproblem:Object-orientedanalysis

10 17-214

Visualizingdynamicbehavior:Interactiondiagrams

•  Aninteractiondiagramisapicturethatshows,forasinglescenarioofuse,theeventsthatoccuracrossthesystem’sboundaryorbetweensubsystems

•  Clarifiesinteractions:–  Betweentheprogramanditsenvironment–  Betweenmajorpartsoftheprogram

•  Forthiscourse,youshouldknowUMLsequencediagrams

11 17-214

Constructingasequencediagram

12 17-214

Anexamplesequencediagram

13 17-214

(Sequencediagramwithnotationannotations)

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);}…

15 17-214

Today

•  Interactiondiagrams:tovisualizedynamicbehavior•  Understandingadesignproblem:Object-orientedanalysis

16 17-214

Ahigh-levelsoftwaredesignprocess

•  Projectinception•  Gatherrequirements•  Defineactors,andusecases•  Model/diagramtheproblem,defineobjects•  Definesystembehaviors•  Assignobjectresponsibilities•  Defineobjectinteractions•  Model/diagramapotentialsolution•  Implementandtestthesolution•  Maintenance,evolution,…

17-313

17-214

17 17-214

Artifactsofthisdesignprocess

•  Model/diagramtheproblem,defineobjects–  Domainmodel(a.k.a.conceptualmodel)

•  Definesystembehaviors–  Systemsequencediagram–  Systembehavioralcontracts

•  Assignobjectresponsibilities,defineinteractions–  Objectinteractiondiagrams

•  Model/diagramapotentialsolution–  Objectmodel

18 17-214

Artifactsofthisdesignprocess

•  Model/diagramtheproblem,defineobjects–  Domainmodel(a.k.a.conceptualmodel)

•  Definesystembehaviors–  Systemsequencediagram–  Systembehavioralcontracts

•  Assignobjectresponsibilities,defineinteractions–  Objectinteractiondiagrams

•  Model/diagramapotentialsolution–  Objectmodel

Today:understandingtheproblem

Definingasolution

19 17-214

Inputtothedesignprocess:Requirementsandusecases

•  Typicallyprose:

20 17-214

Modelingaproblemdomain

•  Identifykeyconceptsofthedomaindescription–  Identifynouns,verbs,andrelationshipsbetweenconcepts–  Avoidnon-specificvocabulary,e.g."system"–  Distinguishoperationsandconcepts–  Brainstormwithadomainexpert

21 17-214

Modelingaproblemdomain

•  Identifykeyconceptsofthedomaindescription–  Identifynouns,verbs,andrelationshipsbetweenconcepts–  Avoidnon-specificvocabulary,e.g."system"–  Distinguishoperationsandconcepts–  Brainstormwithadomainexpert

•  VisualizeasaUMLclassdiagram,adomainmodel–  Showclassandattributeconcepts

•  Real-worldconceptsonly•  Nooperations/methods•  Distinguishclassconceptsfromattributeconcepts

–  Showrelationshipsandcardinalities

22 17-214

Buildingadomainmodelforalibrarysystem

Apubliclibrarytypicallystoresacollectionofbooks,movies,orotherlibraryitemsavailabletobeborrowedbypeoplelivinginacommunity.Eachlibrarymembertypicallyhasalibraryaccountandalibrarycardwiththeaccount’sIDnumber,whichshecanusetoidentifyherselftothelibrary.Amember’slibraryaccountrecordswhichitemsthememberhasborrowedandtheduedateforeachborroweditem.Eachtypeofitemhasadefaultrentalperiod,whichdeterminestheitem’sduedatewhentheitemisborrowed.Ifamemberreturnsanitemaftertheitem’sduedate,thememberowesalatefeespecificforthatitem,anamountofmoneyrecordedinthemember’slibraryaccount.

23 17-214

Buildingadomainmodelforalibrarysystem

Apubliclibrarytypicallystoresacollectionofbooks,movies,orotherlibraryitemsavailabletobeborrowedbypeoplelivinginacommunity.Eachlibrarymembertypicallyhasalibraryaccountandalibrarycardwiththeaccount’sIDnumber,whichshecanusetoidentifyherselftothelibrary.Amember’slibraryaccountrecordswhichitemsthememberhasborrowedandtheduedateforeachborroweditem.Eachtypeofitemhasadefaultrentalperiod,whichdeterminestheitem’sduedatewhentheitemisborrowed.Ifamemberreturnsanitemaftertheitem’sduedate,thememberowesalatefeespecificforthatitem,anamountofmoneyrecordedinthemember’slibraryaccount.

24 17-214

Onedomainmodelforthelibrarysystem

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

26 17-214

BuildadomainmodelforHomework2

27 17-214

PossibledomainmodelsforHomework2

28 17-214

Understandingsystembehaviorwithsequencediagrams

•  Asystemsequencediagramisamodelthatshows,foronescenarioofuse,thesequenceofeventsthatoccuronthesystem’sboundary

•  Designgoal:Identifyanddefinetheinterfaceofthesystem–  System-levelcomponentsonly:e.g.,Auserandtheoverallsystem

29 17-214

Understandingsystembehaviorwithsequencediagrams

•  Asystemsequencediagramisamodelthatshows,foronescenarioofuse,thesequenceofeventsthatoccuronthesystem’sboundary

•  Designgoal:Identifyanddefinetheinterfaceofthesystem–  System-levelcomponentsonly:e.g.,Auserandtheoverallsystem

•  Input:Domaindescriptionandoneusecase•  Output:Asequencediagramofsystem-leveloperations

–  Includeonlydomain-levelconceptsandoperations

30 17-214

Onesequencediagramforthelibrarysystem

Usecasescenario:Alibrarymembershouldbeabletouseherlibrarycardtologinatalibrarysystemkioskandborrowabook.Afterconfirmingthatthememberhasnounpaidlatefees,thelibrarysystemshoulddeterminethebook’sduedatebyaddingitsrentalperiodtothecurrentday,andrecordthebookanditsduedateasaborrowediteminthemember’slibraryaccount.

31 17-214

Formalizesystembehaviorwithbehavioralcontracts

•  Asystembehavioralcontractdescribesthepre-conditionsandpost-conditionsforsomeoperationidentifiedinthesystemsequencediagrams–  System-leveltextualspecifications,likesoftwarespecifications

32 17-214

Asystembehavioralcontractforthelibrarysystem

Operation: borrow(item)Pre-conditions: Librarymemberhasalreadyloggedintothesystem.

Itemisnotcurrentlyborrowedbyanothermember.Post-conditions: Logged-inmember'saccountrecordsthenewly-borrowed

item,orthememberiswarnedshehasanoutstandinglatefee. Thenewly-borroweditemcontainsafutureduedate, computedastheitem'srentalperiodplusthecurrentdate.

33 17-214

Distinguishingdomainvs.implementationconcepts

34 17-214

Distinguishingdomainvs.implementationconcepts

•  Domain-levelconcepts:–  Almostanythingwithareal-worldanalogue

•  Implementation-levelconcepts:–  Implementation-likemethodnames–  Programmingtypes–  Visibilitymodifiers–  Helpermethodsorclasses–  Artifactsofdesignpatterns

35 17-214

Summary:Understandingtheproblemdomain

•  Knowyourtoolstobuilddomain-levelrepresentations–  Domainmodels–  Systemsequencediagrams–  Systembehavioralcontracts

•  Befastand(sometimes)loose–  Elideobvious(?)details–  Iterate,iterate,iterate,…

•  Getfeedbackfromdomainexperts–  Useonlydomain-levelconcepts

top related