Search code examples
classconstructorarchitectureumlclass-diagram

Should we add constructor in the class diagram?


I am designing a class diagram for an application and I was wondering if I should add a constructor to every class. Some classes cannot be created unless they are given an initial value. But other classes, the object can be created without an initial value the user may enter a value or may not. My question is should I include a constructor into every class or just classes that need an initial value?


Solution

  • The key considerations for me when I create diagrams - is it readable, is it clear, and does it give only the relevant information? If you introduce too much clutter on to a diagram, it makes it harder to see the parts that really matter. With that said, if it's in an academic context or if your company has very specific standards, you might do it differently.

    If it were me, and 90% of the classes have a straightforward no-args constructor and 10% do something special, I would show the special cases explicitly and just a UML note element with something like "Unless indicated otherwise, all classes have a default no-arg constructor".