Forums Register Login

Difference between aggregation, association and composition

+Pie Number of slices to send: Send
Hi,
Can someone explain me the difference between aggregation, association and composition with an example within UML Context.
Regards,
Ashok
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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

 
pie. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1958 times.
Similar Threads
Explain Association and other terms
Aggregation vs Composition
what is association,aggregation and composition? what is the difference between them?
Aggregation, association and composition
Aggregation, association and composition
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 00:42:07.