Chapter1220.docx
《Chapter1220.docx》由会员分享,可在线阅读,更多相关《Chapter1220.docx(15页珍藏版)》请在冰豆网上搜索。
Chapter1220
Chapter12
Object-OrientedAnalysisPhase
Theexercisesinthischaptergivestudentstheirfirstpracticalexposuretotheobject-orientedparadigm.Itisimportanttostressthatrepeatediterationisanintrinsicqualityofthisparadigm,andofobject-orientedanalysis(OOA)inparticular.Forexample,problems12.2and12.4seemextremelystraightforward.However,thestudentswillhavetoiterateanumberoftimesbetweenuse-casemodeling,classmodeling,anddynamicmodelingbeforetheyarriveatanacceptablesolution.(Ingeneral,theremaybemorethanoneacceptablesolutiontoagivenproblem.)
problemsolutions
12.1:
ThestatediagramsforclassesButton,Elevator,andElevatorDoorsareshowninFigures12.1,12.2,12.3,and12.4,respectively.
Figure12.1.StatediagramforclassButton.
Figure12.2.StatediagramforclassElevator.
Figure12.3.StatediagramforclassElevatorDoors.
Figure12.4.Use-casediagramforlibrarycirculationsystem.
12.2:
Use-CaseModeling
SeeFigure12.4.Figure12.5showstheexpandedscenarioforcheckingoutabook;theexpandedscenariosforreturningabook,removingabook,addingabook,requestingabooktobeheld,ormakingaqueryaresimilar.
Borrowerhandsbooktobecheckedouttolibrarian.Borrowerpreviouslyhadholdplacedonbook.
1.LibrarianentersCatcomputerterminal,thenscansbookandborrower’scardwithbarcodereader.
2.Book’sborrowernumberisupdated.
3.Holdinformationiscleared
PossibleAlternatives
A.Bookhadnotpreviouslybeenonhold.
Figure12.5.Expandedscenarioforcheckingoutabook.
ClassModeling
Candidateclassesaredeterminedusingnounextraction.
Stage1.ConciseProblemDefinition:
Anautomatedlibrarycirculationsystemistobeconstructed.
Stage2.InformalStrategy:
Anautomatedlibrarycirculationsystemistobeconstructed.Eachbookinthelibrary,aswellaseachborrower,canbeidentifiedbyabar-code.Abookcanbecheckedoutprovideditisnotbeingheldforanotherborrower;atmostoneborrowercanplaceaholdonabookcheckedoutbyanotherborrower.Whenabookisreturned,itischeckedinbyalibrarian.Borrowersandlibrariansarepermittedtoqueryacatalogoflibraryholdings.Librariansmayalsomodifythecatalog.
Stage3.FormalizetheStrategy:
Anautomatedlibrarycirculationsystemistobeconstructed.Eachbookinthelibrary,aswellaseachborrower,canbeidentifiedbyabar-code.Abookcanbecheckedoutprovideditisnotbeingheldforanotherborrower;atmostoneborrowercanplaceaholdonabookcheckedoutbyanotherborrower.Whenabookisreturned,itischeckedinbyalibrarian.Borrowersandlibrariansarepermittedtoqueryacatalogoflibraryholdings.Librariansmayalsomodifythecatalog.
Withregardtothenounsinthepreviousparagraph,borrower,librarian,andbar-codedonotchangewhilethelibraryisoperating;inobject-orientedterminology,theydonothaveaninternalstate.Accordingly,thesenounscannotcorrespondtoclasses.Inaddition,library,hold,holding,andsystemareabstractnouns.Finally,catalogisinformationthatrelatestobooks.ThisleavesBookasthesolecandidateclass.
TheclassdiagramisshowninFigure12.6.
Book
booknumber:
integer
author:
char[41]
title:
char[41]
subject:
char[41]
borrowernumber:
integer
holdnumber:
integer
Figure12.6.Classdiagramforlibrarycirculationsystem.
Figure12.7.Statediagramforlibrarycirculationsystem.
DynamicModeling
Figure12.7showsthestatediagramforclassBook.Itreflectsthevariousstatesthroughwhichabookmaymove,andthetransitionsbetweenthosestates.
Figure12.8.Use-casediagramforATMproduct.
12.3:
AsexplainedinSection11.6ofObject-OrientedandClassicalSoftwareEngineering,FifthEdition,anFSMisaquintuple.Innonmathematicalterms,anFSMconsistsofstatesandtheinputsthatresultinatransitionfromstatetostate.Forobject-orientedmodeling,however,wealsowanttoincorporatetheactionthatistobetakenonenteringastate.
Furthermore,thisshouldappearaspartofthestatediagram.Thissimplifieslaterdevelopment,particularlytheobject-orienteddesignphase.However,theresultingformalismwillnotbeformal,apricewearewillingtopaybecauseOOAasawholeisasemiformaltechnique.
12.4:
Use-CaseModeling
Theuse-caseisshowninFigure12.8.Figure12.9isanexpandedscenarioforwithdrawingfunds;theexpandedscenariosfordeposit,transfer,andbalancerequestaresimilar.
1.UserinsertscardintoATMandthenentersPIN.
2.ATMverifiesthatPINiscorrect.
3.Menuappearsonscreen.
4.Userchoosestowithdrawfunds.
5.Userenters$60asamounttobewithdrawn.
6.Adequatefundsareavailable,somachineupdatesuser’saccountbalance.
ATMdispensesmoney.
7.ATMprintsreceiptshowingnewbalance.
PossibleAlternatives
A.UserentersincorrectPIN.
B.Userrequests$50.
C.Userhaslessthan$60inaccount.
Figure12.9.Expandedscenarioforwithdrawingfunds.
ClassModeling
Thecandidateclassesarechosenusingnounextraction.
Stage1.ConciseProblemDefinition:
Asystemtocontrolanautomatictellermachine(ATM)istobeconstructed.
Stage2.InformalStrategy:
AsystemtocontrolanATMistobeconstructed.Afteracustomer’scardhasbeensuccessfullyverified,thecustomermaydepositandwithdrawmoneyfromanaccount,inquireaboutthebalanceofanaccount,andtransferfundsbetweentwoseparateaccounts.
Stage3.FormalizetheStrategy:
AsystemtocontrolanATMistobeconstructed.Afteracustomer’scardhasbeensuccessfullyverified,thecustomermaydepositandwithdrawmoneyfromanaccount,inquireaboutthebalanceofanaccount,andtransferfundsbetweentwoseparateaccounts.
Withregardtothenounsinthepreviousparagraph,ATM,card,andcustomerdonotchangewhilethesystemisoperating;inobject-orientedterminology,theydonothaveaninternalstate.Also,fundsandmoneyareabstractnouns.Finally,balanceisapropertyofaccount.Thus,thesolecandidateclassisAccount.
TheclassdiagramisshowninFigure12.10.
Account
accountnumber:
integer
balance:
float
PINnumber:
integer
Figure12.10.ClassdiagramforATMproduct.
DynamicModeling
Figure12.11showsastatediagramfortheATMasawholebecauseclassAccountdoesnotmovefromstatetostate.
Figure12.11.StatediagramforATMproduct.
12.5:
Object-orientedanalysisconsistsofthreesteps:
use-casemodeling,classmodeling,anddynamicmodeling.Itispossible,intheory,todothedynamicmodelingstepbeforetheclassmodelingstep.However,thisisnotadvisable,becausewhentheclassesareeventuallyextracted,majorchangesinthestatediagramarealmostcertaintoberequired.Thereasonisthatthestatediagram(essentiallyafinitestatemachine)cannotrepresentoridentifyinheritance.Thus,thelatestpointatwhichclassescanbeintroducedisthestartoftheclassmodeling
12.6:
Itispossible(thoughnotadvisable)tousetherapidprototypeasawayofdeterminingwhattheclassesshouldbe.Thus,classeswouldbeintroducedearlyintherequirementsphase.
Alternatively,classmodelingcouldprecedeuse-casemodelingintheobject-orientedanalysisphase,andclasseswouldbeintroducedattheverybeginningoftheobject-orientedanalysisphase.However,thisapproachwouldprecludeexperienceddevelopersfromextractingtheclassesfromthescenariosorusingCRCcards.
12.7:
Awidevarietyofformalismscouldbeused,bothgraphicalandtextual.Allthatisrequiredisthattheformalismshouldreflectthevariousstates,transitions,andactionsthatareinvolved.
However,suchformalismsarelessconvenienttouse.Forexample,themathematicalformalismofSection11.6ofObject-OrientedandClassicalSoftwareEngineeringislesssuitablethantheoneusedhere.Textualformalismsareequallyunsuitable.
12.8:
Theprincipleofstepwiserefinementrequiresasmanydecisionsaspossibletobepostponed.Alltheoperationsthathavetobeincludedintheproduct(“methods”)musteventuallyappearinthestatediagram.However,assigningthemethodstospecificclassescanwaituntilthedesignphase.Ontheotherhand,modelingbecomesextremelycomplexunlesstheattributesareassignedtotheirspecificclasses.
Infact,thereisanothergoodreasontowaituntilthedesignphasebeforeallocatingmethodstoclasses,namelythatthechoiceofarchitecture(forexample,client-server)willaffectwheretheclassesarephysicallylocated,andhencemayaffecthowtheoperationsareorganizedintomethods,andwhichmethodsshouldbeallocatedtowhichclasses.
termproject
12.9:
Use-CaseModeling
Theuse-caseforupdatingapatientisshowninFigure12.12;acorrespondingexpandedscenarioisshowninFigure12.13.
Figure12.12.Use-casediagramforupdatingapatientrecord.
1.ThenursequeriesthepatientnumberfromtheCPEdatabase
2.Thenurseviewsthepatientrecord.
3.IfthenursenoticesthatthepatientneedsJCFassistancethenurseupdatesthepatientrecordtoreflecttheJCFneed.
4.IfthenursenoticesthatthepatientneedsWFassistancethenurseupdatesthepatientrecordtoreflecttheWFneed.
5.IfthenursenoticesthatthepatientneedsJCassistancethenurseupdatesthepatientrecordtoreflecttheJCneed.
6.Whenthepatientisdischarged,thenurseentersthedischargedate.
7.ThenurseinsertstheupdatedrecordintotheCPEdatabase
PossibleAlternatives
A.Thepatientnumberdoesnotrepresentacurrentlyregisteredpatient.
B.Thenursenoticesthatthepatientdoesnotlivewithin500milesofBACH;suchapatientisnoteli