How can I describe a pointer to class in a UML cla

2019-02-07 03:42发布

问题:

If I have a class A wich contains a pointer to a class B and a method wich 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? Have I to use * ? What kind of composition is it?

回答1:

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.



回答2:

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

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

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.



回答3:

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.

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