3

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.

1
  • 1
    Your last paragraph makes no sense. If A is registered as an Observer to B, an instance of A is passed into the registerObserver() method of B. B may not know about the A's interface, but it must be aware of some object that implements the Observer interface. It's not usually necessary, but could you post some sample code? Show a stub of ClassA and ClassB with the interfaces and abstract classes they inherit from and the method signatures?
    – Thomas Owens
    Apr 28, 2014 at 18:28

2 Answers 2

1

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.

8
  • An association relationship usually means that a class holds a reference to another, or 'knows about it' in some other way. With Observer, the observer and the subject are decoupled meaning they don't know about each other. So is it suitable to draw an association arrow from the observer to the subject, even though the observer doesn't actually know about the subject? (Which is part of the point of this pattern).
    – Aviv Cohn
    Apr 28, 2014 at 17:34
  • 1
    @Prog Association is the generic form of aggregation and composition. Your subject typically maintains a collection of observers, and that's usually represented with aggregation. Since it's a form of association, you can annotate it in the same way.
    – Thomas Owens
    Apr 28, 2014 at 17:41
  • Not sure if I explained myself correctly, I'll explain again what I mean: ClassA implements the Observer interface, ClassB the Subject interface (and it's annotated in the diagram). ClassA is registered as an observer to ClassB (which is an integral part of the design). Drawing an association arrow from ClassA to ClassB, or from ClassB to ClassA, I think would imply that ClassA 'knows about' ClassB, or vice versa - which is wrong. They are totally decoupled from each other. But it is important in order to understand the design, to see that ClassA is an observer of ClassB.
    – Aviv Cohn
    Apr 28, 2014 at 17:54
  • So can I draw this arrow from A to B, even though A doesn't actually 'know about' B?
    – Aviv Cohn
    Apr 28, 2014 at 17:56
  • 1
    @Prog:UML does not cover every situation that you want to document in a way that you want to document it. It is a very rare project where people aren't "inventing" their own ways of using UML to communicate what they mean. YES, that is what UML was supposed to resolve but UML took a turn away from being a communication tool to being more CASE Tool designer friendly many years ago. That means that people in the real world have to either use "Notes" in the diagrams or hijack existing features for their own meaning. If it is important to understanding then show it in an adhoc way that makes sense
    – Dunk
    Apr 28, 2014 at 20:39
1

UML defines a special diagram type for showing how class instances are connected to each other: the Object diagram.

An object diagram uses a similar notation to a class diagram, but rather than structural relations between types, it shows how objects are relating to each other at some point in time.

In your concrete case, you would have two diagrams:

  • A class diagram showing how ClassA is an Observer and ClassB a Subject in the Observer Pattern, and
  • an object diagram showing the relevant instances of ClassA and ClassB with a dependency between them (dashed arrow with open arrow head) with the stereotype <<observes>>. This diagram then shows that a particular instance of ClassA at some point in time is connected to a particular instance of ClassB through the observer pattern.

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.