Chapter 12
Object-Oriented Design
Nowthatyouknowsomedatastructuringtechniques,it’s timetostretchyourwingsandreallyputthose
toolstowork.Mostmoderncomputerapplicationsaredesignedusinga data-centeredviewofcomputing.
Thisso-calledobject-orienteddesign(OOD)processis apowerfulcomplementtotop-downdesignforthe
developmentofreliable,cost-effective softwaresystems.Inthischapter, wewilllookat thebasicprinciples
ofOODandapplythemina couplecasestudies.
12.1 TheProcessofOOD
Theessenceofdesignisdescribinga systemintermsofmagicalblackboxesandtheirinterfaces. Each
componentprovidesa setofservicesthroughitsinterface. Othercomponentsareusersorclientsofthe
services.
A clientonlyneedsto understandtheinterfaceofa service;thedetailsofhow thatserviceis implemented
arenotimportant.Infact,theinternaldetailsmaychangeradicallyandnotaffecttheclientat all.Similarly,
thecomponentprovidingtheservicedoesnothave toconsiderhow theservicemightbeused.Theblackbox
justhastomake surethattheserviceis faithfullydelivered.Thisseparationofconcernsis whatmakesthe
designofcomplex systemspossible.
Intop-downdesign,functionsserve theroleofourmagicalblackboxes. Aclientprogramcanusea
functionaslongasit understandswhatthefunctiondoes.Thedetailsofhowthetaskis accomplishedare
encapsulatedinthefunctiondefinition.
Inobject-orienteddesign,theblackboxesareobjects.Themagicbehindobjectsliesinclassdefinitions.
Oncea suitableclassdefinitionhasbeenwritten,wecancompletelyignorehowtheclassworksandjustrely
ontheexternalinterface—themethods.Thisis whatallowsyoutodraw circlesingraphicswindowswithout
somuchasa glanceat thecodeinthegraphicsmodule.Allthenitty-grittydetailsareencapsulatedinthe
classdefinitionsforGraphWinandCircle.
If wecanbreaka largeproblemintoa setofcooperatingclasses,wedrasticallyreducethecomplexity
thatmustbeconsideredtounderstandany givenpartoftheprogram.Eachclassstandsonitsown.Object-
orienteddesignis theprocessoffindinganddefininga usefulsetofclassesfora givenproblem.Like all
design,it is partartandpartscience.
Therearemany differentapproachestoOOD,eachwithitsownspecialtechniques,notations,gurusand
textbooks. I can’t pretendtoteachyouallaboutOODinoneshortchapter. Ontheotherhand,I’mnot
convincedthatreadingmany thickvolumeswillhelpmucheither. Thebestwaytolearnaboutdesignis to
doit.Themoreyoudesign,thebetteryouwillget.
Justtogetyoustarted,herearesomeintuitive guidelinesforobject-orienteddesign.
- Lookforobjectcandidates.Yourgoalis todefinea setofobjectsthatwillbehelpfulinsolvingthe
problem.Startwitha carefulconsiderationoftheproblemstatement.Objectsareusuallydescribed
bynouns.Youmightunderlineallofthenounsintheproblemstatementandconsiderthemoneby
one.Whichofthemwillactuallyberepresentedintheprogram?Whichofthemhave “interesting”
behavior?Thingsthatcanberepresentedasprimitive datatypes(numbersorstrings)areprobablynot