12

If I have a class A which contains a pointer to a class B and a method which takes in input a pointer to class B

class A {
private:
    B* attribute;
public:
    void method(B* par);
}

how can I describe it in a UML class diagram? Do I have to use *? What kind of composition is it?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Maghio
  • 355
  • 1
  • 6
  • 18
  • 1
    What type of UML diagrams are you using? Class diagram (most popular)? https://creately.com/blog/diagrams/uml-diagram-types-examples/ – Avt Feb 16 '14 at 21:15
  • I have written class diagram. – Maghio Feb 16 '14 at 21:17
  • I understand your problem, but it is difficult to answer without painting UML diagram itself :) – Avt Feb 16 '14 at 21:20
  • I just want to know if I have to write the * symbol and what kind of composition I have to use. I'm using a program to make the diagram but it doesn't allow me to use * symbol =( – Maghio Feb 16 '14 at 21:22
  • are you wondering between composition and aggregation? When A owns B, ie. it does the new and delete on the B pointer it would usually be composition, if B is made somewhere else and A gets a reference to it, then it is usually considered aggregation afaik – Emile Vrijdags Feb 16 '14 at 21:42
  • In my case A contains a pointer to B but A CREATE and DELETE B. Is it a composition? – Maghio Feb 16 '14 at 21:44
  • Have I to write * in the class diagram? – Maghio Feb 16 '14 at 21:46

3 Answers3

24

In UML it is not as important to show whether it is a pointer or not. Why? Because, you may be using UML describing an OOD for a language without pointers.

Quick answer: from your code, A aggregates B (empty diamond at A class, connecting B with a solid line). That is because there is no destructor that deletes what A.attribute pointer references.

What is really important is to say what is the lifetime of the referenced object. So, for a referenced object (has relationship) that dies when the owner is destroyed, you have to use a solid (filled) diamond. This is called composition. So the owner has to manage the life time of the owned object. One such example is human has hands. The hands do not survive when the human object is destroyed.

When the diamond is not filled (aggregation), then the owner is not responsible to manage the life of the object owned. For example you will not expect to see that the owned object being deleted in the destructor. An employer has a TeamLeadRole, but when the employer is "destroyed" (i.e. left the company) then the TeamLeadRole is still available.

Now, traditionally when you see a filled diamond, you usually (not all the time) will have an object by value. Where when you see an empty diamond you may use reference, or pointer.

If your class uses another class but does not keep instances (or references/pointers) to that class, then you can denote dependency by just a simple line (solid) between the objects. That means there is a relationship is called association.

Trimtab
  • 377
  • 2
  • 5
  • 1
    I'm using C++ wich is full of pointers, so I just write the name of the class, without *? – Maghio Feb 16 '14 at 22:05
  • 2
    Yes, you do not write the *. UML is a tool to communicate ideas, not to exactly show a specific implementation in a specific language. You want to illustrate/communicate the abstract relation (has a reference) between the objects/classes. Again, it is not to show how to implement it a specific language (c++ using pointers in your case). – Emile Vrijdags Feb 16 '14 at 22:18
  • @Trimtab: “Now, traditionally when you see a filled diamond, you usually (not all the time) will have an object by value. Where when you see an empty diamond you may use reference, or pointer.”: that's not always true. As far as I know, you may also say you have a filled diamond when both ends disappears together, and not filled for the other case. – Hibou57 Feb 18 '14 at 14:52
3

A plain C++ pointer directly corresponds to a reference property in a UML class diagram, as shown in the following diagram:

enter image description here

Such a reference property expresses a uni-directional (binary) association, as shown in the following diagram:

enter image description here

Notice the "dot" at the B side of the association line. It means that the association end with name "attribute" is owned by class A, which means that it corresponds to the reference property shown in the diagram above (the association can be replaced with the reference property). For more explanation see Chapter 5 of this tutorial.

You cannot use the C++ symbol "*" in the UML class diagram because

  1. it is Cpp-specific, while your diagram should probably be platform-independent;
  2. there is no need to use such a symbol in UML because it's clear that the attribute references B objects;
  3. this symbol has a different meaning in UML (the multiplicity unbounded).

In any case, the relationship between the classes A and B is an association. It may be a composition, if you have the additional semantics of an aggregate-component relationship. Notice that your question should be worded: "What kind of association is it?" instead of "What kind of composition is it?" because composition and aggregation are special types of associations.

Gerd Wagner
  • 5,481
  • 1
  • 22
  • 41
  • How can I write vectors, unsigned types or files? – Maghio Feb 16 '14 at 23:02
  • If you mean the case where an attribute/property has a C++ vector of references as its value, this corresponds to a multi-valued reference property or to an association end with multiplicity *. For unsigend types and files you should better write another (complete) question. – Gerd Wagner Feb 16 '14 at 23:16
  • No, I mean the case of a function wich takes in input a vector – Maghio Feb 16 '14 at 23:34
  • I guess that for representing a C++ member function of a class with a vector-valued in-parameter in UML you would define a custom datatype class "Vector" and an operation with an in-parameter of type "Vector". – Gerd Wagner Feb 16 '14 at 23:51
2

There IS the possibility to difference between *, & and [] fields on UML class diagrams. It is named "Type Modifier". The problem is to show it on the diagram for your tool. If you have a diagramming tool, simply draw it at the end as a stereotype of the end. But be careful not to mix it with the multiplicity! If you have a modelling tool, you"ll have to look how to do it.

But notice, that pointer/reference field is supposed to be a default one, so you needn't really specify your case in the class diagram.

If you want to show this information in a correct way, use a more detailed Composite Structure Diagram. There you can show these &,*,[] at you wish.

enter image description here

For different variants of association in class diagram look this my answer for the similar question: https://stackoverflow.com/a/21478862/715269

Community
  • 1
  • 1
Gangnus
  • 24,044
  • 16
  • 90
  • 149