josh bloch charlie garrod - carnegie mellon school of...
Post on 06-Jun-2020
2 Views
Preview:
TRANSCRIPT
1 15-214
SchoolofComputerScience
PrinciplesofSo3wareConstruc9on: Objects,Design,andConcurrencyDesigning(sub-)systemsAformaldesignprocess,part2JoshBloch CharlieGarrod
2 15-214
Administrivia
• MidtermexamThursday– ReviewsessionWednesday,September28th,7-9pm,HHB103
• Homework4– Threeparts,partAdueOct6th– Designreviewmee9ngsnextweek
• Collabora9onpolicy…
3 15-214
KeyconceptsfromlastThursday…
4 15-214
Sequenceandcommunica9ondiagrams
5 15-214
Designprinciples
• Lowcoupling• Lowrepresenta9onalgap• Highcohesion
6 15-214
Buildingadomainmodelforalibrarysystem
Apubliclibrarytypicallystoresacollec9onofbooks,movies,orotherlibraryitemsavailabletobeborrowedbypeoplelivinginacommunity.Eachlibrarymembertypicallyhasalibraryaccountandalibrarycardwiththeaccount’sIDnumber,whichshecanusetoiden9fyherselftothelibrary.Amember’slibraryaccountrecordswhichitemsthememberhasborrowedandtheduedateforeachborroweditem.Eachtypeofitemhasadefaultrentalperiod,whichdeterminestheitem’sduedatewhentheitemisborrowed.Ifamemberreturnsanitema3ertheitem’sduedate,thememberowesalatefeespecificforthatitem,anamountofmoneyrecordedinthemember’slibraryaccount.
7 15-214
Onedomainmodelforthelibrarysystem
8 15-214
Notesonthelibrarydomainmodel
• Allconceptsareaccessibletoanon-programmer• TheUMLissomewhatinformal
– Rela9onshipsareo3endescribedwithwords• Real-world"is-a"rela9onshipsareappropriateforadomainmodel• Real-wordabstrac9onsareappropriateforadomainmodel• Itera9onisimportant
– Thisexampleisafirstdra3.Someterms(e.g.Itemvs.LibraryItem,Accountvs.LibraryAccount)wouldlikelyberevisedinarealdesign.
• Aggregatetypesareusuallymodeledasclasses• Primi9vetypes(numbers,strings)areusuallymodeledasadributes
9 15-214
BuildadomainmodelforMonopoly
10 15-214
BuildadomainmodelforMonopoly
Monopolyisagameinwhicheachplayerhasapiecethatmovesaroundagameboard,withthepiece’schangeinloca9ondeterminedbyrollingapairofdice.Thegameboardconsistsofasetofproper9es(ini9allyownedbyabank)thatmaybepurchasedbytheplayers.Whenapiecelandsonapropertythatisnotowned,theplayermayusemoneytobuythepropertyfromthebankforthatproperty’sprice.Ifaplayerlandsonapropertyshealreadyowns,shemaybuildhousesandhotelsontheproperty;eachhouseandhotelcostssomepricespecificfortheproperty.Whenaplayer’spiecelandsonapropertyownedbyanotherplayer,theownercollectsmoney(rent)fromtheplayerwhosepiecelandedontheproperty;therentdependsonthenumberofhousesandhotelsbuiltontheproperty.Thegameisplayedun9lonlyoneremainingplayerhasmoneyandproperty,withalltheotherplayersbeingbankrupt.
11 15-214
Ar9factsofthisdesignprocess
• Model/diagramtheproblem,defineobjects– Domainmodel(a.k.a.conceptualmodel)
• Definesystembehaviors– Systemsequencediagram– Systembehavioralcontracts
• Assignobjectresponsibili9es,defineinterac9ons– Objectinterac9ondiagrams
• Model/diagramapoten9alsolu9on– Objectmodel
Understandingtheproblem
Definingasolu9on
12 15-214
Ar9factsofthisdesignprocess
• Model/diagramtheproblem,defineobjects– Domainmodel(a.k.a.conceptualmodel)
• Definesystembehaviors– Systemsequencediagram– Systembehavioralcontracts
• Assignobjectresponsibili9es,defineinterac9ons– Objectinterac9ondiagrams
• Model/diagramapoten9alsolu9on– Objectmodel
Today
13 15-214
Understandingsystembehaviorwithsequencediagrams
• Asystemsequencediagramisamodelthatshows,foronescenarioofuse,thesequenceofeventsthatoccuronthesystem’sboundary
• Designgoal:Iden9fyanddefinetheinterfaceofthesystem– Twocomponents:Auserandtheoverallsystem
14 15-214
Understandingsystembehaviorwithsequencediagrams
• Asystemsequencediagramisamodelthatshows,foronescenarioofuse,thesequenceofeventsthatoccuronthesystem’sboundary
• Designgoal:Iden9fyanddefinetheinterfaceofthesystem– Twocomponents:Auserandtheoverallsystem
• Input:Domaindescrip9onandoneusecase• Output:Asequencediagramofsystem-levelopera9ons
– Includeonlydomain-levelconceptsandopera9ons
15 15-214
Onesequencediagramforthelibrarysystem
Usecasescenario:Alibrarymembershouldbeabletouseherlibrarycardtologinatalibrarysystemkioskandborrowabook.A3erconfirmingthatthememberhasnounpaidlatefees,thelibrarysystemshoulddeterminethebook’sduedatebyaddingitsloanperiodtothecurrentday,andrecordthebookanditsduedateasaborrowediteminthemember’slibraryaccount.
16 15-214
Onesequencediagramforthelibrarysystem
Usecasescenario:Alibrarymembershouldbeabletouseherlibrarycardtologinatalibrarysystemkioskandborrowabook.A3erconfirmingthatthememberhasnounpaidlatefees,thelibrarysystemshoulddeterminethebook’sduedatebyaddingitsloanperiodtothecurrentday,andrecordthebookanditsduedateasaborrowediteminthemember’slibraryaccount.
17 15-214
BuildonesystemsequencediagramforMonopoly
Usecasescenario:Whenaplayerlandsonanunownedpropertyandhasenoughmoneytobuytheproperty,sheshouldbeabletobuythepropertyfortheproperty’sprice.Thepropertyshouldnolongerbepurchasablefromthebankbyotherplayers,andmoneyshouldbemovedfromtheplayertothebank.
18 15-214
Formalizesystembehaviorwithbehavioralcontracts
• Asystembehavioralcontractdescribesthepre-condi9onsandpost-condi9onsforsomeopera9oniden9fiedinthesystemsequencediagrams– System-leveltextualspecifica9ons,likeso3warespecifica9ons
19 15-214
Asystembehavioralcontractforthelibrarysystem
Opera9on: borrow(item)Pre-condi9ons: Librarymemberhasalreadyloggedintothesystem.
Itemisnotcurrentlyborrowedbyanothermember.Post-condi9ons: Logged-inmember'saccountrecordsthenewly-borrowed
item,orthememberiswarnedshehasanoutstandinglatefee. Thenewly-borroweditemcontainsafutureduedate, computedastheitem'srentalperiodplusthecurrentdate.
20 15-214
Dis9nguishingdomainvs.implementa9onconcepts
21 15-214
Dis9nguishingdomainvs.implementa9onconcepts
• Domain-levelconcepts:– Almostanythingwithareal-worldanalogue
• Implementa9on-levelconcepts:– Implementa9on-likemethodnames– Programmingtypes– Visibilitymodifiers– Helpermethodsorclasses– Ar9factsofdesignpaderns
22 15-214
Summary:Understandingtheproblemdomain
• Knowyourtoolstobuilddomain-levelrepresenta9ons– Domainmodels– Systemsequencediagrams– Systembehavioralcontracts
• Befastand(some9mes)loose– Elideobvious(?)details– Iterate,iterate,iterate,…
• Getfeedbackfromdomainexperts– Useonlydomain-levelconcepts
23 15-214
Drawadomainmodelforcryptarithmsolving
24 15-214
Ar9factsofourdesignprocess
• Model/diagramtheproblem,defineobjects– Domainmodel(a.k.a.conceptualmodel)
• Definesystembehaviors– Systemsequencediagram– Systembehavioralcontracts
• Assignobjectresponsibili9es,defineinterac9ons– Objectinterac9ondiagrams
• Model/diagramapoten9alsolu9on– Objectmodel
Understandingtheproblem
Definingasolu9on
25 15-214
Object-orientedprogramming
• Programmingbasedonstructuresthatcontainbothdataandmethods
publicclassBicycle{privateintspeed;privatefinalWheelfrontWheel,rearWheel;privatefinalSeatseat;…publicBicycle(…){…}publicvoidaccelerate(){speed++;}publicintspeed(){returnspeed;}}
26 15-214
Responsibilityinobject-orientedprogramming
• Data:– Privateorotherwiseencapsulateddata– Dataincloselyrelatedobjects
• Methods:– Privateorotherwiseencapsulatedopera9ons– Objectcrea9on,ofitselforotherobjects– Ini9a9ngac9onsinotherobjects– Coordina9ngac9vi9esamongobjects
27 15-214
Usinginterac9ondiagramstoassignobjectresponsibility
• Foragivensystem-levelopera9on,createanobjectinterac9ondiagramattheimplementa6on-levelofabstrac9on– Implementa9on-levelconcepts:
• Implementa9on-likemethodnames• Programmingtypes• Helpermethodsorclasses• Ar9factsofdesignpaderns
28 15-214
Exampleinterac9ondiagram#1
Usecasescenario:Alibrarymembershouldbeabletouseherlibrarycardtologinatalibrarysystemkioskand…
29 15-214
Exampleinterac9ondiagram#2
Usecasescenario:…andborrowabook.A3erconfirmingthatthememberhasnounpaidlatefees,thelibrarysystemshoulddeterminethebook’sduedatebyaddingitsloanperiodtothecurrentday,andrecordthebookanditsduedateasaborrowediteminthemember’slibraryaccount.
30 15-214
Interac9ondiagramshelpevaluatedesignalterna9ves
Createtwopossibleinterac9ondiagrams:1. Solvingacryptarithm,assumingthatthecryptarithmclasshas
responsibilityforsolvingitself2. Solvingacryptarithm,assumingthatthemainmethod(oradelegated
methodorclass)hasresponsibilityforsolvingthecryptarithm
31 15-214
Heuris9csforresponsibilityassignment
• Controllerheuris9c• Informa9onexpertheuris9c• Creatorheuris9c
Goals
Heuristics Patterns
Principles
32 15-214
Thecontrollerheuris9c
• Assignresponsibilityforallsystem-levelbehaviorstoasinglesystem-levelobjectthatcoordinatesanddelegatesworktootherobjects– Alsoconsiderspecificsub-controllersforcomplexuse-casescenarios
• Designprocess:Extractinterfacefromsystemsequencediagrams– Keyprinciples:Lowrepresenta9onalgapandhighcohesion
33 15-214
Informa9onexpertheuris9c
• Assignresponsibilitytotheclassthathastheinforma9onneededtofulfilltheresponsibility– Ini9aliza9on,transforma9on,andviewsofprivatedata– Crea9onofcloselyrelatedorderivedobjects
34 15-214
Responsibilityinobject-orientedprogramming
• Data:– Privateorotherwiseencapsulateddata– Dataincloselyrelatedobjects
• Methods:– Privateorotherwiseencapsulatedopera9ons– Objectcrea9on,ofitselforotherobjects– Ini9a9ngac9onsinotherobjects– Coordina9ngac9vi9esamongobjects
35 15-214
Informa9onexpertheuris9c
• Assignresponsibilitytotheclassthathastheinforma9onneededtofulfilltheresponsibility– Ini9aliza9on,transforma9on,andviewsofprivatedata– Crea9onofcloselyrelatedorderivedobjects
• Designprocess:Assignmentfromdomainmodel– Keyprinciples:Lowrepresenta9onalgapandlowcoupling
36 15-214
Usetheinforma9onexpertheuris9c
• InHomework3,whatobjectshouldhavetheresponsibilitytosolveacryptarithm?
• Whatistherelevantinforma9on?
37 15-214
Usetheinforma9onexpertheuris9c
• InHomework3,whatobjectshouldhavetheresponsibilitytosolveacryptarithm?
• Whatistherelevantinforma9on?– Whoknowsthe#ofdigits(e.g.base10)inthecryptarithm?– Whoknowstheledersofthecryptarithm?– Whocanevaluatethecryptarithmexpressionstocheckforequality?
38 15-214
Anotherdesignprinciple:Minimizeconceptualweight
• Labeltheconceptsforaproposedobject– Relatedtorepresenta9onalgapandcohesion
39 15-214
Creatorheuris9c:WhocreatesanobjectFoo?
• Assignresponsibilityofcrea9nganobjectFootoaclassthat:– Hasthedatanecessaryforini9alizinginstancesofFoo– Contains,aggregates,orrecordsinstancesofFoo– CloselyusesormanipulatesinstancesofFoo
• Designprocess:Extractfromdomainmodel,interac9ondiagrams– Keyprinciples:Lowcouplingandlowrepresenta9onalgap
40 15-214
Usethecreatorheuris9c
• InHomework3,whatobjectshouldhavetheresponsibilityforcrea9ngthepermuta9ongenerator?
41 15-214
Object-levelar9factsofthisdesignprocess
• Objectinterac9ondiagramsaddmethodstoobjects– Caninferaddi9onaldataresponsibili9es– Caninferaddi9onaldatatypesandarchitecturalpaderns
• Objectmodelaggregatesimportantdesigndecisions– Isanimplementa9onguide
42 15-214
Crea9nganobjectmodel
• Extractdata,methodnames,andtypesfrominterac9ondiagrams– Includeimplementa9ondetailssuchasvisibili9es
43 15-214
44 15-214
Createanobjectmodelforyourcryptarithmsolver
45 15-214
Summary:
• Object-levelinterac9ondiagramsandobjectmodelsystema9callyguidethedesignprocess– Convertdomainmodel,systemsequencediagram,andcontractsto
object-levelresponsibili9es
• Useheuris9cstoguide,butnotdefine,designdecisions• Iterate,iterate,iterate…
top related