Java Reference
In-Depth Information
//*****************************
public static void main(String[] args)
{
// Create two objects of class Dog
Dog dog1 = new Dog("Fido",2, 12.95,"Spaniel");
Dog dog2 = new Dog("Atila",3, 20.75,"Hound");
// Display pet data using host class and client class
// methods
dog1.getName();
// Method in host class
dog2.getName();
// Method in host class
dog2.getLocation();
dog2.getSpecData(); // Method in client class
}
}
Aggregation
IntheprogramPetStore2,listedpreviously,theclassDogisabletoaccess
themethodsoftheclassPetthroughanobjectoftheclasspetcalledaPet.
Theobjectiscreatedinthefollowingstatement:
PetaPet=newPet();
InasensetheobjectaPetisadummyobject,sinceitservesmerelyto
accessmethodsinthehostclass.Incaseslikethiswesaythattherelation-
shipbetweentheclientandthehostclassisoneofaggregation.Inmanyof
theothersampleprogramslistedpreviouslyinthisbook,thedrivingclass
createsobjectsofotherclassesinordertoaccesstheirmethods.Aggrega-
tionisthesimplestandmostdirectwayofaccessingthepublicmembers
ofanotherclass.
Thesimplicityanddirectnessof aggregationassociations doesnot
makeitanylessvaluable.Inmanycasesaggregationprovidesanalterna-
tivetoclassinheritanceavoidingsomeofthepitfallsofthemorecomplex
model.Aggregationisoftendepictedinclassdiagramsbymeansofanar-
rowpointingfromtheclientclasstothehost,asshownin Figure17-2 .
Window
width
height
aRectangle
Rectangle
area()
winArea()
Figure 17-2 Modeling Aggregation
 
Search WWH ::




Custom Search