2

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?

  • 2
    Maybe I'm missing something, but is there a need to show this on the UML diagram? I'm not that familiar with .NET, but I've never had a need to show a Java annotation on a UML class diagram. – Thomas Owens Apr 24 '15 at 13:57
  • @ThomasOwens The presence of a particular attribute provides a mapping between two class types which I thought might be useful to illustrate on the diagram. So far the best that I can come up with is to draw a dependency line with the attribute name on it. Perhaps that IS the right way? – Lea Hayes Apr 24 '15 at 15:25
  • That seems appropriate, but I'd have to have a better understanding of how .NET annotations work. – Thomas Owens Apr 24 '15 at 16:06
3

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   |
    +----------------------+
  • Nice one! I think I prefer the UML 1.x method over the UML 2.x; seems redundant having to re-specify <<SuperSpecial>>. – Lea Hayes Jan 5 '16 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. – Ramon Rivas Jan 5 '16 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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