Search code examples
javaobserver-patterndesign-patterns

java.util.Observable does not follow GoF's observer design pattern, right?


If I understand correctly, in the observer design pattern, a concrete observer has the concrete subjet as a field. This is as illustrated in GoF's book:

enter image description here

On the other hand, in the implementation of notifyObserver(Object arg) of java.util.Observable , the subject is passed again to the observer (See source code )

enter image description here

If the concrete subject is supposed to be referable through a field in the concrete observer (as in the GoF's spec), why are we sending the concrete subject again to the observer (as in java.util.observable code) This seems to suggest me that java.util.Observable does not follow the GoF's spec. Have I misunderstood something here? Thanks.


Solution

  • Yes, the implementations differ.

    BUT!

    The pattern is the concept, not the implementation. And the concept is the same. In both cases the observer has a reference to the observed item, but you could definitely also imagine a situation where the observer does not need this reference at all.

    So the concept here is "Something gets notified when something else changes state or executes in some way", and it's much broader than any of the implementations.