Re: Aggregation vs composition

From: Michael Rauscher (michlmann_at_gmx.de)
Date: 04/20/04


Date: Tue, 20 Apr 2004 21:58:15 +0200

Hi Matthias,

Matthias Hofmann schrieb:

[snip]
>>No, that contradicts not. Take a look at a car. It has an engine. You

First, I have to correct a grammar mistake: "That doesn't contradict"...

>>can remove the engine before you destroy the car. The engine can exist
>>alone. But if you destroy the car without having the engine removed, the
>>engine will also be destroyed.
>>
>
>
> I think I understand: the "0..1" indicates that the part can also exist
> without a whole, i.e. as a stand-alone object. Unfortunately, many authors
> seem to write books about the UML without having a precise idea about it.
> Maybe this is because the UML is still an emerging standard that requires a
> couple more years to really establish itself.

The problem is, that some people intrepret missing informations as they
want to. E.g. some people think, that if the multiplicity isn't given
explicitly, it's 1. Some think, that if they don't see a class, it isn't
here. That's wrong: if the multiplicity isn't given, it's just not shown.

[...]

>>No. You aggregated persons with houses. Person as the whole - House as
>>the part.
>
>
> Oh, I think I see the misunderstanding: I thought that the whole is said to
> build its parts, but you were trying to say that the parts kind of
> constitute (or "build") their whole. I guess the confusion arouse because
> often in C++, an object creates (or "builds") its parts in the constructor.

Now I know where the misunderstanding came from ;-)

>>No. A person may own or live in a house. You can say the person has a
>>house. But you cannot say the house is part of a person (like the nose
>>is - construction?).
>>
>
>
> I think you are right. In my example, one has to create an association and
> name it something like "is owner of". A better example for aggregation would

That association would be the way I'd do it.

> have been a company and its employees:
>
> |Company|<>----------|Employee|
>
> The company can be destroyed, but the employees can still sit around till
> they find a new employer. Also, an employee can work for more than a single
> company, and the employees can exist without the company.

I'd model that also as an association named "works for"...

>
>
>>>Of course the "has a" relationship in my example is supposed to express
>>>ownership rather than physical containment.
>>
>>That's the difference. E.g. an invoice "consists" of many positions. You
>>can say (perhaps in german only), that
>>
>>- an invoice contains it's positions
>>- positions build the invoice
>>- a position is part of an invoice
>>
>>So there's a part-whole relationship between invoice and position. But
>>if you destroy the invoice you also destroy the positions (construction?)
>>
>
>
> But that is another example for composition, not aggregation, isn't it?

Yep.

> Composition is easy to explain, but the concept of aggregation in the UML
> seems a bit vague. Physical containment is probably always a case of
> composition.

The definitions are relatively clear. Often, the problem is the point of
view. E.g. you say, that staff builds up a company whereas I'd say, that
staff just works for a company. It depends on the context.

Let's discuss another model. Imagine a vector drawing application.
Select some objects. How would you model the relation between the
selection and the selected objects?

>
> Maybe the difference between aggregation and composition is easier to
> understand if you realize the there are actually also two different kinds of
> construction or destruction. Destroying a car is different from destroying a
> company. The former requires a big hammer, the latter requires a legal act.
> Only the former results in physical destruction, which makes it a good
> candiate for composition.

May be. If, given a whole-part-relationship, destroying the whole,
unavoidable involve destruction of it's parts, it's a composition.

>
> In summary, I think aggregation and composition differ in only 2 points:
>
> - Only in aggregation a part can be shared by more than one whole
> - Only in composition the part is destroyed together with the whole unless
> it is removed before

The First point is true, the last isn't. Remove the word "Only" since
you may destroy the parts in an aggregation too (of course).

>
> I guess the C++ std::vector and other containers are an example of
> composition, because the container's destructor frees all the objects it
> contains. Also, an object cannot be in more than a single container at a
> time.

That has somewhat of logic ;-)

>
> But what if I store pointers to objects in a std::vector? The pointers
> itself will be destroyed with the container, but this is not so with the
> pointed-to objects. Also, I can store the same pointer value in more than
> one container. Now, is this aggregation? I think it is, if you regard the
> pointed-to object as being the thing that counts. And as the UML is an
> object oriented language, it probably cares more about objects than
> pointers.

I think you're right.

As mentioned: don't think in implementation levels. As you see you can
use std::vector to implement composition, aggregation and association. A
language construct just might indicate the type of relation.

>
> Any thoughts?
>
> Regards,
>
> Matthias
>
>

Bye
Michael



Relevant Pages

  • Re: Aggregation vs composition
    ... But if you destroy the car without having the engine removed, ... >> aggregation involves construction, I just thought that the aggregation ... Composition is easy to explain, but the concept of aggregation in the UML ... But what if I store pointers to objects in a std::vector? ...
    (comp.object)
  • Re: Mapping Class Diagram to coding
    ... Aggregation (Weak Composition). ... Even though BFactory is the *only* class allowed to destroy B objects, ... CAR changes its wheel-cup each time when it go to service the car. ...
    (comp.object)
  • Re: Aggregation - Composition
    ... Now I understand the difference between aggregation and composition but what ... a car can't exist without an engine ... You can say that the course "aggregates" its students, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Implementation of Aggregation
    ... If it is aggregation, then an object relates to another, but when one ... They relate to each other because a person can own a car, ... without the engine the car performs no meaningful operations. ... when an engine is destroyed, it should destroy the car as well. ...
    (comp.software.patterns)
  • Re: Aggregation vs composition
    ... responsibility is shared because the object is shared. ... Is this not Composition, simply because Car is ... may destroy the part. ... >> other language that I'm aware of. ...
    (comp.object)