UML Class Diagram – Illustrating Attributes/Annotations

annotationsattributesclass-diagramuml

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?

Best Answer

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   |
    +----------------------+