Can someone explain me the difference between aggregation, association and composition with an example within UML Context.
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