Java Reference
In-Depth Information
Insteadofthinkinginheritance,wecanconsidertheprobleminsim-
plerterms.Firstwenotethatthereareoperationsthatarethesamefor
dogs,cats,andbirds.Toavoidduplicationandwastedeffortwecreatea
class,calledPet,tohandlethesecommonoperations.Second,thefunc-
tionsthatarespecifictodogs,cats,andbirdsareimplementedinthere-
spectiveclasses.Third,thefielddatafortheobjectsislocatedinthe
clientclasses.Theresultisthatclientshavetotalcontrolovertheirob-
jectsandcanexpandtheirfunctionalityasneeded.Theclassdiagramof
Figure16-4 ismodifiedaccordingly,asshownin Figure17-1 .
Pet
getName()
getLocation()
getPrice()
Dog
Cat
Bird
name
location
price
birdColor
aPet
name
location
price
dogRace
aPet
name
location
price
catRace
aPet
getSpecData()
getSpecData()
getSpecData()
Figure17-1AlternativeClassDiagramforthePetStoreSystem
Comparingtheinheritance-basedclassdiagramin Figure16-4 withthe
onein Figure17-1 ,youwillnoticethefollowingdifferences:
The arrow that connects the classes Dog, Cat, and Bird with the class Pet
does not depict an inheritance relationship. The absence of the semicircle
symbol indicates that the classes are not in a “is a kind-of”relationship.
Since there is no inheritance, the class Pet is no longer an abstract class nor
is the method getSpecData() defined in the class Pet.
 
Search WWH ::




Custom Search