Reputation: 157
I am confused to distinguish composition and aggregation from code. For example, from the following code, which part of code indicates their relationship?
public class Account {
private Owner owner;
public Account(Owner owner) {
this.owner = owner;
}
}
public Owner {...}
Upvotes: 0
Views: 92
Reputation: 24464
shared aggregation
is named simply aggregation
, but it is simply an error. For shortening you can name it shared
.)So, what you have can be named as shared aggregation or none aggregation (the latter the better). But you have one more possibility. What you have here, is the classifier owned end
. When one class has instance(s) of another one by reference or directly. In UML standard it is also named atribute
. And is shown as dotted end.
Also, you have rights not to show navigation arrows, dots, multiplicity and aggregation. (you have no obligation to show ALL possible info on the diagram - such lightest form of the class diagram is usually the start variant)
So , you can use the following variants:
On the right side you can use empty diamond, arrow, dot, or any combination of them. The B variant is the best, IMHO. And C is OK, too.
You can call your association shared aggregation or none aggregation. You should call your association a classifier owned/dotted one.
Upvotes: 2
Reputation: 41137
Whether a relationship is an aggregation or a composition is a choice you make in design.
It is common with this sort of relation for an Owner
object to be (or perhaps have) either an aggregation or a composition of multiple Account
objects, and the distinction is whether deletion of an Owner
object forces the deletion of its Account
objects, i.e., whether the lifetime of the Account
objects is dependent on the lifetime of the owning Owner
object.
It's also common for an Account
object to have a link back to its owning Owner
object as in your code, but I think most people would not regard that link as a either a composition or an aggregation. It's nice in that case for the Owner
object to actually be a composition of Account
objects, and that the field owner
in Account
is not allowed to be null. But it's not necessarily so. Your choice...
What happens in the "real world" is irrelevant. What really matters is how you choose to model it.
Upvotes: 2
Reputation: 2279
Its a Composition, As your Account
object must have an owner
associated with it.
If you will allow your Account object creation without any owner associated with it then its a aggregation.
But as per common business logic, it may be always desirable to exist an Account with an owner. An account without an owner will have no sense.
Composition is stricter version of Aggregation. Aggregation simply states "has a" but you can think of Composition as stricter or must to have to create an object (in your case Account
object)
Upvotes: 1