12.4. OOCONCEPTS 221
self.dice[i].setColor("gray")
else: # User clicked Roll or Score
for d in self.dice: # Revert appearance of alldice
d.setColor("black")
if b == "Score": # Score clicked, ignorechoices
return[]
elif choices!= []: # Don’t accept Roll unlesssome
returnchoices # dice are actually selected
Thataboutwrapsupourprogram.Theonlymissingpieceofourinterfaceclassis theclosemethod.
To closeupthegraphicalversion,wejustneedtoclosethegraphicswindow.
def close(self):
self.win.close()
Finally, weneeda fewlinestoactuallygetourgraphicalpokerplayingprogramstarted. Thiscodeis
exactlylike thestartcodeforthetextualversion,exceptthatweuseaGraphicsInterfaceinplaceof
theTextInterface.
inter = GraphicsInterface()
app = PokerApp(inter)
app.run()
We now have a complete,useablevideodicepoker game.Ofcourse,ourgameis lackinga lotofbellsand
whistlessuchasprintinga niceintroduction,providinghelpwiththerules,andkeepingtrackofhighscores.
I have triedtokeepthisexamplerelativelysimple,whilestillillustratingimportantissuesinthedesignof
GUIsusingobjects.Improvementsareleftasexercisesforyou.Have funwiththem!
12.4 OOConcepts
Mygoalfortheracquetballandvideopokercasestudieswastogive youa tasteforwhatOODis allabout.
Actually, whatyou’ve seenis onlya distillationofthedesignprocessforthesetwo programs.Basically, I have
walkedyouthroughthealgorithmsandrationalefortwo completeddesigns.I didnotdocumenteverysingle
decision,falsestartanddetouralongtheway. Doingsowouldhave atleasttripledthesizeofthis(already
long)chapter. Youwilllearnbestbymakingyourowndecisionsanddiscoveringyourownmistakes,notby
readingaboutmine.
Still,thesesmallishexamplesillustratemuchofthepowerandallureoftheobject-orientedapproach.
HopefullyyoucanseewhyOOtechniquesarebecomingstandardpracticeinsoftwaredevelopment.The
bottom-lineisthattheOOapproachhelpsustoproducecomplex softwarethatis morereliableandcost-
effective. However, I stillhave notdefinedexactlywhatcountsasobjected-orienteddevelopment.
MostOOgurustalkaboutthreefeaturesthattogethermake developmenttrulyobject-oriented:encapsu-
lation,polymorphismandinheritance. I don’t wantto belabortheseconceptstoomuch,but yourintroduction
toobject-orienteddesignandprogrammingwouldnotbecompletewithoutatleastsomeunderstandingof
whatis meantbytheseterms.
12.4.1 Encapsulation.
I have alreadymentionedthetermencapsulationinpreviousdiscussionofobjects. Asyouknow, objects
know stuff anddostuff. They combinedataandoperations.Thisprocessofpackagingsomedataalongwith
thesetofoperationsthatcanbeperformedonthedatais calledencapsulation.
Encapsulationis oneofthemajorattractionsofusingobjects.It providesa convenientwaytocompose
complex problemsthatcorrespondstoourintuitive view ofhow theworldworks.We naturallythinkofthe
worldaroundusasconsisitingofinteractingobjects.Eachobjecthasitsownidentity, andknowingwhat
kindofobjectit is allowsustounderstanditsnatureandcapabilities.I lookoutmywindow andI seehouses,
carsandtrees,nota swarmingmassofcountlessmoleculesoratoms.