user3143433
user3143433

Reputation: 157

Is this implementation a composition or aggregation?

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

Answers (3)

Gangnus
Gangnus

Reputation: 24464

  • There are three kinds of aggregation - none, shared and composed.
  • The composed aggregation, or composition is defined strictly in UML standard - when the life of the item instance is limited by life of container or association, it IS composition. So, it could be your case? But the composition also, according to standard, supposes association multiplicities one-to many (1 on one end, 0..* on the other one), and you haven't it here! And you are addressing container from the item, it is impossible for composition!
  • Shared aggregation is not defined strictly. Usually it is about one class having instances of another one. But it is not a rule, merely a tradition. And you have this case. Sou, you CAN name it shared aggregation. But you needn't.
    • (Often shared aggregation is named simply aggregation, but it is simply an error. For shortening you can name it shared.)
  • None aggregation is used when you have no composition and you have decided that you have no shared aggregation. So, you can use it, too.

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:

enter image description here

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

Don Roby
Don Roby

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

sakura
sakura

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

Related Questions