UML Class Diagram – How to Represent Constants in UML Class Diagram?

cclass-diagramheadersuml

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

Best Answer

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.