Software Engineering
uml class-diagram annotations attributes
Updated Sun, 18 Sep 2022 22:16:49 GMT

How can attributes/annotations be illustrated on a UML class diagram?


Many programming languages offer a way to annotate types.

For instance, in .NET this can be achieved by deriving a custom attribute class from System.Attribute and then annotating another type with this:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class SuperSpecialAttribute : Attribute { ... }
[SuperSpecial]
public class MyClass { ... }

My question is; how can I show that MyClass has the attribute SuperSpecialAttribute on a UML class diagram?




Solution

You could represent the annotation as a stereotype, and then apply the stereotype to the class.

    +----------------------+
    |   <<SuperSpecial>>   |
    |       MyClass        |
    +----------------------+

If your annotation has attributes, you can represent them as tagged values on the class. Tagged attributes have different syntax in UML 1.x and UML 2.x

In UML 1.x you put tagged values between {}, like this:

    +----------------------+
    |   <<SuperSpecial>>   |
    | {AllowMultiple=true} |
    |       MyClass        |
    +----------------------+

In UML 2.x you can use a section for the stereotype attributes:

    +----------------------+
    |   <<SuperSpecial>>   |
    |       MyClass        |
    +----------------------+
    | <<SuperSpecial>>     |
    | AllowMultiple=true   |
    +----------------------+




Comments (2)

  • +0 – Nice one! I think I prefer the UML 1.x method over the UML 2.x; seems redundant having to re-specify <<SuperSpecial>>. — Jan 05, 2016 at 19:02  
  • +1 – @Lea Hayes The advantage of having a separate section is if your class has more than one stereotype applied. Your tagged values will be clearly associated with the right stereotype. — Jan 05, 2016 at 19:28