Reputation: 119
Say I have a relationship like this
Class Lion{
private int health;
public void eat(Food f){
health+=f.getweight();
}
}
Class Food{
private int weight;
}
Then, am I right to say that the relationship is an association but not an aggregation or composition.
And can I say that Lion uses Food and draw the UML Diagram like this
+-----+ +-----+
| Lion| _____uses___>|Food |
+-----+ +-----+
Also this relationship would have no multiplicity because Lion does not have an array of Food, neither does food have an Array of Lions as instance variables. Unlike in composition and aggregation where the relationship has a multiplicity.
What if I wish for Food to use Lion in the same way too. Do I Draw two arrows between the two classes?
Upvotes: 0
Views: 1185
Reputation: 3496
In fact, Food is not a Property or an Attribute of Lion, so the relation between them is not an Association. The fact that you can not specify the multiplicity is also another hint for saying that the relation between Lion and Food is not an Association.
Your Lion Class uses Food to eat so you can think about the UML Usage relationship. But according to UML Specification "A Usage is a Dependency in which the client Element (aka Lion) requires the supplier Element (aka Food) for its full implementation or operation. This is not your case here, you just need to know that the Food exist, you do not really care about its implementation.
So in fact if Lion knows what is Food , you do not need any relationship between them (Cf screenshot ).
Just if your Lion does not know what is Food you need an ElementImport relation between them.
Upvotes: 0
Reputation: 36295
A <<uses>>
relation is a dependency and not an association, which makes much sense with Lion
and Food
since the association would have been gone once the Food
is swallowed by the Lion
.
As for the aggregation: this is about the lifetime of objects. A shared aggregation has no defined semantics and shall only be used after the domain has defined it. A composite aggregation tells that the aggregated object dies along with the object aggregating it. That would not really make sense. If the Lion
is shot the Food
is still there.
I would not know how Food
would make use of the Lion
at all.
Upvotes: 1