Search code examples
javascriptdesign-patternsobserver-patternpublish-subscribe

Are there situations where the observer pattern is better than the publish subscriber pattern?


I am reading http://addyosmani.com/resources/essentialjsdesignpatterns/book/#designpatternsjavascript to learn more about design patterns.

I would now like to know if there is a situation where an observer pattern can be better than the publish-subscriber-pattern?

In my opinion the publish-subscriber-pattern is much more flexible and easier.

Regards


Solution

  • A situation could be when implementing a drag and drop handler.

    Observing "ondrag" and "ondrop" is needlessly indirect. When dispatching "ondrop", imagine having to call every object which is subscribed to "ondrop". It could be upwards to millions.