Python Programming: An Introduction to Computer Science

(Nora) #1
202 CHAPTER12. OBJECT-ORIENTEDDESIGN

importantcandidatesforobjects.Thingsthatseemtoinvolve a groupingofrelateddataitems(e.g.,
coordinatesofa pointorpersonaldataaboutanemployee)probablyare.


  1. Identifyinstancevariables.Onceyouhave uncoveredsomepossibleobjects,thinkabouttheinforma-
    tionthateachobjectwillneedtodoitsjob. Whatkindsofvalueswilltheinstancevariableshave?
    Someobjectattributeswillhave primitive values;othersmightthemselvesbecomplex typesthatsug-
    gestotherusefulobjects/classes.Strive tofindgood“home”classesforallthedatainyourprogram.

  2. Thinkaboutinterfaces.Whenyouhave identifieda potentialobject/classandsomeassociateddata,
    thinkaboutwhatoperationswouldberequiredforobjectsofthatclasstobeuseful.Youmightstart
    byconsideringtheverbsintheproblemstatement.Verbsareusedtodescribeactions—whatmustbe
    done.Listthemethodsthattheclasswillrequire.Rememberthatallmanipulationoftheobject’s data
    shouldbedonethroughthemethodsyouprovide.

  3. Refinethenontrivialmethods.Somemethodswilllooklike they canbeaccomplishedwitha couple
    linesofcode.Othermethodswillrequireconsiderableworktodevelopanalgorithm.Usetop-down
    designandstepwiserefinementtofleshoutthedetailsofthemoredifficultmethods.Asyougoalong,
    youmayverywelldiscoverthatsomenewinteractionswithotherclassesareneeded,andthismight
    forceyoutoaddnew methodstootherclasses.Sometimesyoumaydiscovera needfora brand-new
    kindofobjectthatcallsforthedefinitionofanotherclass.

  4. Designiteratively. Asyouworkthroughthedesign,youwillbouncebackandforthbetweendesigning
    newclassesandaddingmethodstoexistingclasses.Workonwhateverseemstobedemandingyour
    attention. Noonedesignsa programtoptobottomina linear, systematicfashion. Make progress
    wherever it seemsprogressneedstobemade.

  5. Tryoutalternatives.Don’t beafraidtoscrapanapproachthatdoesn’t seemtobeworkingortofollow
    anideaandseewhereit leads. Gooddesigninvolvesa lotoftrialanderror. Whenyoulookatthe
    programsofothers,youareseeingfinishedwork,nottheprocessthey wentthroughtogetthere.If a
    programis welldesigned,it probablyis nottheresultofa firsttry. FredBrooks, a legendarysoftware
    engineer, coinedthemaxim:“Plantothrowoneaway.” Oftenyouwon’t reallyknowhowa system
    shouldbebuiltuntilyou’ve alreadybuiltit thewrongway.

  6. Keepit simple.Ateachstepinthedesign,trytofindthesimplestapproachthatwillsolve theproblem
    at hand.Don’t designinextracomplexityuntilit is clearthata morecomplex approachis needed.


Thenextsectionswillwalkyouthrougha couplecasestudiesthatillustrateaspectsofOOD.Onceyou
thoroughlyunderstandtheseexamples,youwillbereadyto tackleyourownprogramsandrefineyourdesign
skills.


12.2 CaseStudy:RacquetballSimulation


Forourfirstcasestudy, let’s returntotheracquetballsimulationfromChapter9.Youmightwanttogoback
andreview theprogramthatwedevelopedthefirsttimearoundusingtop-downdesign.
Thecruxoftheproblemis tosimulatemultiplegamesofracquetballwheretheabilityofthetwo op-
ponentsisrepresentedbytheprobabilitythatthey wina pointwhenthey areserving. Theinputstothe
simulationaretheprobabilityforplayerA,theprobabilityforplayerB,andthenumberofgamestosimu-
late.Theoutputis a nicelyformattedsummaryoftheresults.
Intheoriginalversionoftheprogram,weendeda gamewhenoneoftheplayersreacheda totalof 15
points. Thistimearound,let’s alsoconsidershutouts. Ifoneplayergetsto7 beforetheotherplayerhas
scoreda point,thegameends.Oursimulationshouldkeeptrackofboththenumberofwinsforeachplayer
andthenumberofwinsthatareshutouts.

Free download pdf