Object-oriented – UML class diagram: how to annotate observer-subject relationship

object-orientedobserver-patternuml

How can I annotate in a UML class diagram that an instance of ClassA is registered as an observer of an instance of ClassB? (Both implement the appropriate interfaces).

This doesn't really belong in a class diagram, because they are static, but if this is something that is important to the design – how can you annotate it in this type of diagram?

Edit:

To clarify: I know how to annotate the Observer pattern in UML. My question is more specific.

ClassA being registered as an observer to ClassB is important for the design, however I'm not sure if an association relationship between the two classes is appropriate, since ClassA and ClassB don't know about each other. They're totally decoupled, and only connected through the Observer pattern.

Best Answer

Documenting this type of relationship is suitable for a class diagram. You can add role names to the association links between classes, similar to how it's demonstrated on Scott Ambler's introduction to UML class diagrams or the Wikipedia example of Associations. I would also indicate the fact that they implement the interfaces or inherit from the base class, even if you are using a language like Java that provides the necessary classes (although I would consider omitting the details for these language-provided classes).

You can also apply notes in the UML diagram. Most tools that I've used allow for attaching notes to classes. These could annotate the usage of a pattern or even provide for a pseudocode implementation of a few functions, if it doesn't lead to clutter on the diagram.

Related Topic