Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I describe a pointer to class in a UML class diagram?

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?

like image 404
Maghio Avatar asked Feb 16 '14 21:02

Maghio


1 Answers

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.

like image 184
Trimtab Avatar answered Sep 27 '22 23:09

Trimtab