Forums Register Login

composition and aggregation

+Pie Number of slices to send: Send
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.

------------------
+Pie Number of slices to send: Send
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).


for Composition


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 l�onard
www.evisor.com
+Pie Number of slices to send: Send
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.

They love to discuss this stuff in the OO forum, for example:
http://www.javaranch.com/ubb/Forum9/HTML/000303.html
A discussion at "some other site" on composition and aggregation http://artima.com/flexiblejava/fjf/compoinh/messages/24.html
+Pie Number of slices to send: Send
thanx cindy, it really helped me.
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
Based on this article by Jeff Friesen, http://www.javaworld.com/javaworld/jw-06-2001/jw-0608-java101.html, from a Java standpoint there appears to be no difference.
However, from a UML standpoint, there is only a slight difference. "Composition is exactly like Aggregation except that the lifetime of the 'part' is controlled by the 'whole'." Check out http://www.ootips.org/uml-hasa.html for details.
Hope this helps,
John
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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).]
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1797 times.
Similar Threads
Association, Aggregation and composition implementation in java
Implementing UML Concepts in Java
association, aggregation, Composition
1.3Associations vs Compositions
how to map these in java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:56:05.