This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

composition and aggregation

 
moeen amir
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
moeen amir
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx cindy, it really helped me.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic