Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UML class diagram Association vs ( Aggregation | Composition )-Diamonds

Tags:

uml

I am not sure if I do use the association and aggregation or composition diamond properly.

I would use the Association for interfaces, because I can't instantiate them. Like they do it here for example. Or for static classes, same reason.

And the diamonds I use only for objects I can instantiate. Like normal classes.

But I am not sure if this is the correct way to differentiate them, because if you check again, you will see they aren't so specific about it. In the UML 2.3 specification I couldn't get out more, so how are you using it?

And there is a third manner, the dashed lined <> arrow, but I don't have a glue when to use this one. So maybe you can help me with that one, too?

like image 229
Christian Avatar asked Oct 20 '11 09:10

Christian


People also ask

What does diamond mean in UML class diagram?

It signifies aggregation. From wikipedia: In UML, it is graphically represented as a hollow diamond shape on the containing class end of the tree with a single line that connects the contained class to the containing class.

What is association and aggregation in class diagram?

Aggregation describes a special type of an association which specifies a whole and part relationship. Association is a relationship between two classes where one class use another. Diamond shape structure is used next to the assembly class.

What is the difference between aggregation and composition in class diagram?

In an aggregation relationship, the associated objects exist independently within the scope of the system. In a composition relationship, the associated objects cannot exist independently within the scope of the system. In this, objects are linked together. In this, the linked objects are independent of each other.

What are the four types of relationships in UML?

Since it is termed as a link, it demonstrates how things are interrelated to each other at the time of system execution. It constitutes four types of relationships, i.e., dependency, association, generalization, and realization.


1 Answers

I would use the Association for interfaces, because I can't instantiate them. Like they do it here for example. Or for static classes, same reason.

And the diamonds I use only for objects I can instantiate. Like normal classes.

That's not really how they work. The three forms (Association, Aggregation and Composition) define different properties about a relationship. All three are normally used between classes although can relate Interfaces too. Association and Composition are the two easiest:

  • Association (no diamond) is the most general form, allowing cardinality and navigability to be defined at both ends.
  • Composition (filled diamond) is a whole-part relationship where the 'whole' (end with the black diamond) 'contains' the part. It imposes two key restrictions:
    1. There can only be 1 container (i.e. cardinality at whole end is exactly 1);
    2. It imposes a lifecycle responsibility for parts on the whole. So the container is responsible for creating and deleting parts. A part cannot continue to exist if its container is deleted.

Aggregation (unfilled diamond) sits somewhere in the middle. It's a bit like composition - except it doesn't mandate the properties described above. I don't personally use it. The semantics are too unclear for it to be worthwhile.

And there is a third manner, the dashed lined <> arrow, but I don't have a glue when to use this one.

I think you mean the dependency relationship. It's a weaker form of association. As an example, take the following class definition

class Foo {

 def bar(Baz: aParam) {
  ...
 }
}

In this case type Foo has a dependency on type Baz from its use in the bar() method signature. However there's no association between them (can't sensibly discuss e.g. cardinality of relationship between an instance of Foo and an instance of Baz).

From a practical perspective I'd say:

  • you can use straight Associations for 80%+ of the relationships you're likely to want to model
  • Composition probably accounts for most of the remaining scenarios
  • Dependency can be useful in some circumstances
  • You can get by quite happily without ever using Aggregation.

hth.

like image 118
sfinnie Avatar answered Nov 12 '22 14:11

sfinnie