3

Commonly I see class diagrams where there is no <<class>> stereotype. Others where there is the common <<interface>> and others with some interesting ones as <<shape>> or <<computer>> and so which for a class diagram I personally don't see the necessity but Ok.

I'm making an UML diagram for a modern algebra project in C++, but as we know the language shouldn't be so influential when working UML. For this project I have recognized the <<interface>> and <<abstract>> stereotypes all fine until this point, but there are structs and classes, I want to do things in the best way possible so I came to ask myself are <<class>> and <<struct>> valid stereotypes?

I kindly thank your answers or comments.

Note: I think struct should be valid because it specifies the type for a certain implementation.

1 Answer 1

4

You don't need to indicate to indicate classes explicitly in a class diagram: it's assumed by default, according to UML:

A Class is shown using the Classifier symbol. As Class is the most widely used Classifier, no keyword is needed to indicate that the metaclass is Class.

You are free to define your own stereotypes in UML. They are best documented in a profile diagram that shows how they relate to standard UML elements.

UML is implementation language neutral. It is very typical to define language specific data types (e.g. float, double which do not belong to standard UML) and stereotypes (e.g. class and struct). See for example here a C# profile, for a language in which classes and structs have different semantics.

Be aware that not everything between « », which looks like a stereotype, is a stereotype. It can be a well-defined keyword as well:

  • interface is a keyword that refers to something with a clear meaning in the UML metamodel, which is shared between class diagrams and component diagrams.
  • enumeration is a keyword that refers to a special kind of type, which needs to be described differently than other basic data types and classes.

Two very important remarks in addidition:

  • Introducing language-specific stereotypes makes your design model less general, and bears the risk to get detailed implementation models rather than focusing on the abstract design. You should therefore think about the pros and cons. If your model is language neutral, you could focus on OOD and let it be reused for a java or a C# port for example. If your primary intent is to making an implementation model, it’s of course not a problem.
  • In C++ a struct is a class with all members public by default. This is a core language principle. The difference is only about access control. Since in UML you document access control at member level, having a stereotype « struct » that requires all members to be public seems just redundant. I’d advise to use instead unstereotyped classes in UML and decide in the implementation to use a struct or a class depending on member visibility.
5
  • You could as well point out that UML uses stereotypes the same way as keywords (Annex C, p 743 of UML 2.5), though both are different. In my view a design flaw in UML.
    – user188153
    Aug 15, 2020 at 9:36
  • And on a second note: struct would be a valid stereotype, but structured is already a keyword!
    – user188153
    Aug 15, 2020 at 9:40
  • @qwerty_so Excellent suggestion! Thanks: I edited and I think it's even clearer now :-)
    – Christophe
    Aug 15, 2020 at 10:22
  • @qwerty_so btw, there is indeed some inconsistency in the UML specs: some places suggest that keywords and stereotypes it is something different (e.g. "keywords or stereotypes" p.39 or "Stereotype names should not clash with keyword names" p.278), in other places it suggest that it's closely related (e.g."keywords (including stereotype names)" p.99). I suppose this is historically grown and caused by the fact that some keywords mean to metaclasses that can be extended with stereotypes.
    – Christophe
    Aug 15, 2020 at 10:28
  • 1
    They improved the specs quite to some extend. But of course it's not flawless. There is a page where you can report bugs. But mills grind very slowly at OMG. I reported a few bugs and every time the old ones still were not fixed. I just lost sight (and interest). As long as it's not getting worse :-/
    – user188153
    Aug 15, 2020 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.