donderdag 19 augustus 2010

UML: association, aggregation, composition

I have studied about these three relationships and I think I fully understand them now.

We can say an association is a relationship between two objects. An aggregation is a specialized form of association and a composition is a specialized form of aggregation. So we can say both aggregation and composition is an association:


















Some more information about the relationships, then I will show you how I determine what relationship objects have.

Association:
One relationship between objects
Objects have their own life cycle
No ownership

Aggregation (has-a, hollow diamond):
Objects have their own life cycle
Has ownership (child object cannot belong to another parent object)

Composition (part-of, filled diamond):
Objects has life time dependency, no own life cycle
Has ownership (child object cannot belong to another parent object)

How to determine what relationship objects have? I've figured it out through asking questions, so I have invented this diagram which maybe can help you:

















 Here is the explanation for this diagram:


1. Is it a part of …? Does it need this part to have a whole, complete thing?
E.g. an engine is a part of a car, because a car without an engine is not complete. This is the same for book and page, computer and hard disk and order and order line.

 This is not applicable for farm and farmer; we know without a farmer the farm will cease as nobody take care for the animals, grains etc… so maybe this will make you think a farmer is a part of a farm, but it is not! A farm is an area of land in which food is produced and managed, that's it, there is no need to think more complicated. This means a farm can exists without having a farmer, the same is a farmer can exists even there is no farm.

2. Is there ownership? Does it belong to…?
E.g. a movie has a review, a review belongs to a movie, and it does not belong to any other movie. The same can be applied to farm and farmer, as a farmer has a farm and the farm belongs to that farmer and not to other farmers. Both can exist without each other.

3. An association
E.g. a student and a course is an association. A student has a course, but a course does not belong to a student. Another example, a person and a project, a person has a project but a project does not belong to a person (unless it's a 1 person project with no other persons involved).



Hopefully you now understand how to determine the relationships. Let's do some exercises, since theory should be put in practice in order to learn:

1. Body and arm = ?
2. Department and teacher?
3. Teacher and student?
4. Project and project leader = ?
5. Order and orderline = ?
6. School and student = ?
7. Customer and order = ?
8. Project and student = ?

Answers (highlight to read)
1. Composition
2. Aggregation
3. Association
4. Composition
5. Composition
6. Aggregation
7. Aggregation
8. Aggregation

3 opmerkingen: