aspose file tools*
The moose likes Beginning Java and the fly likes composition and aggregation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "composition and aggregation" Watch "composition and aggregation" New topic
Author

composition and aggregation

moeen amir
Greenhorn

Joined: Feb 06, 2001
Posts: 4
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.

------------------
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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


"JavaRanch, where the deer and the Certified play" - David O'Meara
moeen amir
Greenhorn

Joined: Feb 06, 2001
Posts: 4
thanx cindy, it really helped me.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
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).]
John Sinues
Ranch Hand

Joined: Feb 21, 2000
Posts: 52
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
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
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.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
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).]
 
jQuery in Action, 2nd edition
 
subject: composition and aggregation