how can i implement the composition and aggregation in java. how can i differenciate between them, in java. any details, any code examples plz. thanx in advance.
Joined: Nov 22, 2008
Hi! That'a BIG a Question ! i follow some month ago the Object-Oriented Application Analysis and Design for java Tecnology by Sun. The teacher was not confident for the difference between the aggregation and composition. I think there is no rules to implement on one way these relations; for Agregation:
An agregation is a whole/part relationship, but the parts me be shared with other objects, and may also have life time outside the scope of the whole. .......
he told us it's typically a reference set to an object(Given by a setter).
The use of Composition depends on how strongly the reliationship is emphazed within the system.If the concept of Engine has been separated from the concept of the car...... But the rules of the system state the car cannot exist witout an Engine, so you must use composition.
it is translate in the code that Engine is created inside the constructor of car (most of time);
I know this is not exactly a clear response. But i hope it helps
Benjamin, I thought that your answer was quite good. I had no problem understanding it. In the case of composition the object is created with the new operator inside the class and the reference placed in a variable. In the case of aggregation the object was created somewhere else and passed into the class using a method which then puts the reference into a variable.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Joined: Feb 06, 2001
thanx cindy, it really helped me.
Joined: Nov 14, 2000
The code examples given above are only vaguely right or vaguely wrong, since Java or ... are vague on that part too. Actually, the engine created inside car still can still live outside of the car, since you can write a method to make it available outside world. Actually Factory Method pattern is generally create object instance inside the factory and use the instance outside in general!
The point I want to make is that the difference between aggregation and composition is OO concept/disign differences, not simple code difference like the above!!! JavaChina has been moved to http://javachina.developergroup.org/ [This message has been edited by Roseanne Zhang (edited September 25, 2001).] [This message has been edited by Roseanne Zhang (edited September 25, 2001).]
Originally posted by Cindy Glass: In the case of composition the object is created with the new operator [b]inside the class and the reference placed in a variable. In the case of aggregation the object was created somewhere else and passed into the class using a method which then puts the reference into a variable.[/B]
I'm no expert on the subject, but I don't think this always applies. A car need not, and probably should not create it's own engine. It can take an engine in the constructor, and may have one constructor the creates a default engine. I believe this would still qualify as composition.
Joined: Nov 14, 2000
It is a lot obvious in C++.
The relationship between Chapter with Book are composition since when Book is destroyed, the Chapters will be destroyed no matter what. However, The relationship between Reference with Book are aggregation, since the other books or websites referenced by the book will not be destroyed when destructor of Book is called. You know that. SCJD Study Group has been moved to http://www.developergroup.org/ [This message has been edited by Roseanne Zhang (edited September 26, 2001).]