GeeCON Prague 2014*
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes Difference between aggregation, association and composition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "Difference between aggregation, association and composition" Watch "Difference between aggregation, association and composition" New topic
Author

Difference between aggregation, association and composition

Ashok Chakravarthy
Greenhorn

Joined: Feb 03, 2004
Posts: 2
Hi,
Can someone explain me the difference between aggregation, association and composition with an example within UML Context.
Regards,
Ashok
Priya Patel
Greenhorn

Joined: Jan 30, 2004
Posts: 23
Association
Association is fundamentally a "relationship" between classes. In its simplest form it is shown as a single line connecting two classes (for 3 or more associations you would use the UML concept of N-ARY associations) If the lines connecting two classes do not have any connecting arrows it means that the classes are jointly navigable, i.e one can navigate from one class to the other. A simple association would be :-
places
Customer--------------------Order

Rolenames can be added to the above and would translate to an attribute of the defined type. Code generation tools would generally do this automatically and prefix with "my", "an" "the", i.e
Works for
Employee---------------------------Company
worker employer

Would translate into the following Java Classes :-


Aggregation/Composite
These are just an advanced form of an association. They model whole/part contexts.

An aggregate relationship means that the "Part" can exist without the whole. An example of this would be :-
Portfolio---------------Stock
You could have many different "portfolios" in which "stock" could be a member of , in which case this is an aggregate relationship.

An example of a composition would be :-
Order-------OrderLine
i.e one order can have 1 or more order lines. If the order is removed, the order lines are destroyed also. A composite relationship means that the parts can have only one who. An example would be a Book (the whole) containing many parts (pages, index, contents page, cover etc). A book can have one and only one contents page --> Composite relationship. Composites are modelled via a dark diamond. The diamond appears on the "Whole".

HTH
Priya Patel
Greenhorn

Joined: Jan 30, 2004
Posts: 23
Just noticed that my "Spaces" were stripped out ..
so : "Places" is an association description for :-
Customer--------------------Order
"Customer places an Order"
and :-

"Employee Works for a Company"
Where "Worker" is a Rolename for Employee and
"Employer" is a Rolename for Company
Ashok Chakravarthy
Greenhorn

Joined: Feb 03, 2004
Posts: 2
Hi,
Thanks for the explaination.
Just to have more clarity, In your below example "Portfolio" has a relationship with "stocks" which can be called as association (of type aggregation). So when would we define as association and not an aggregation? Is it that only if it deal with whole/parts (car 'whole' contains engine,wheels as 'part') we consider to be of type aggregation. And in case we have customer placing an order, can we call it an association (of type composition customer <>---- order).
Do let me know if the above understanding is correct.
Regards,
Ashok
Originally posted by Priya Patel:
Association
Association is fundamentally a "relationship" between classes. In its simplest form it is shown as a single line connecting two classes (for 3 or more associations you would use the UML concept of N-ARY associations) If the lines connecting two classes do not have any connecting arrows it means that the classes are jointly navigable, i.e one can navigate from one class to the other. A simple association would be :-
places
Customer--------------------Order

Rolenames can be added to the above and would translate to an attribute of the defined type. Code generation tools would generally do this automatically and prefix with "my", "an" "the", i.e
Works for
Employee---------------------------Company
worker employer

Would translate into the following Java Classes :-
class Company
{
Employee myWorker;
}
class Employee
{
Company myEmployer;
}

Aggregation/Composite
These are just an advanced form of an association. They model whole/part contexts.

An aggregate relationship means that the "Part" can exist without the whole. An example of this would be :-
Portfolio---------------Stock
You could have many different "portfolios" in which "stock" could be a member of , in which case this is an aggregate relationship.

An example of a composition would be :-
Order-------OrderLine
i.e one order can have 1 or more order lines. If the order is removed, the order lines are destroyed also. A composite relationship means that the parts can have only one who. An example would be a Book (the whole) containing many parts (pages, index, contents page, cover etc). A book can have one and only one contents page --> Composite relationship. Composites are modelled via a dark diamond. The diamond appears on the "Whole".

HTH

 
GeeCON Prague 2014
 
subject: Difference between aggregation, association and composition