7

I am working on developing a UML class diagram from C++ code. I have a classless header file that is basically a definitions file (it has structs, enums and defined constants). I know how to represent structs and enums in general, but I am unclear about how to represent (or if I should represent) constants in the class diagram. Additionally I am curious if the structs, enums and constants should be part of some larger container? (Like referencing the namespace helper or the definitions.h)

#ifndef DEFINITIONS_H
#define DEFINITIONS_H

#include <stdint.h>
#include <string>

namespace helper
{

  enum Colors
  {
    Red = 0,
    Blue,
    Green   
  };

  struct volunteer
  {
    std::string FirstName;
    std::string MiddleName;
    std::string LastName;
    std::string Occupation;
  };


  typedef const std::string Constant;
  Constant Foo = "Foo";
  Constant Bar = "Bar";
  Constant Bat = "Bat";
 }

#endif
ÁEDÁN
  • 133
  • 1
  • 1
  • 6

4 Answers4

5

The goal of a class diagram is to document relationships between classes as well as how objects of those classes can change:

In software engineering, a class diagram in the Unified Modeling Language (UML) is a type of static structure diagram that describes the structure of a system by showing the system's classes, their attributes, operations (or methods), and the relationships among objects.

Source: Wikipedia

The key elements here are:

  • Relationships between classes: given a class, what other classes does it use?
  • Operations that can be performed on a class: what methods/functions belong to a class?
  • State belonging to a class: what data does a class encapsulate?

A constant does not fit into any of these elements. It is not a relationship, and certainly not an operation. The closest element is state, but a constant is pretty much the opposite: it is static, and not tied to an object. While a class diagram documents classes, it is focused on elements of classes used by objects of those class types.


In your specific example, I would do the following:

  • The enum would be a class in the diagram, but would likely be empty. The only state is the integer that represents each enum value, but that is essentially a surrogate key and not referenced in code. Note that if you use a C++ enum class instead, you might have state worth documenting.

  • The struct should be a class with no behavior, but the state documented as public.

  • Those constants should not be documented in a UML class diagram. For one, they do not belong to a class, making them completely irrelevant. Two, constants do not belong in a class diagram anyway for reasons I outlined above.

  • I would create a separate UML package diagram for namespace helper (as an aside, I recommend picking a more descriptive name for this namespace) showing that the enum and struct are in the namespace. This diagram type also allows for documenting static members in general and constants specifically.

  • UML has an own metatype `Enumeration` (see p. 165 of the UML 2.5 specs) and this should be used, not an empty class. A `struct` should preferable be stereotyped with `<>`. –  Jul 22 '17 at 21:19
3

A class diagram is supposed to show an entity level model. A constant is a minor implementation detail and logically, it is not a member of the class, it bears no meaning in the OO view of things. So the answer is "you don't".

Martin Maat
  • 18,218
  • 3
  • 30
  • 57
1

Some of your helper information could be modeled as such:

enter image description here

The notion of a constant in UML exists, but it's usually inside a class and has a type. See the Employee class in the diagram above.

Fuhrmanator
  • 1,435
  • 9
  • 19
0

I would represent them as objects (or "instances" in IBM Rational terminology) of class Constant or std::string (whichever seems more appropriate). The object symbol can have the value added below the name. You could mark the value as {read-only} if desired.

Simon B
  • 9,167
  • 4
  • 26
  • 33