bodokaiser
bodokaiser

Reputation: 15752

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

Upvotes: 0

Views: 194

Answers (2)

Kernel James
Kernel James

Reputation: 4074

Another situation is handling menus and sub-menus. When a sub-menus is closed, do you publish an "on-menu-close" topic? If so, then all menus will close included nested and non-nested menus. In reality you only want the last menu to close.

Upvotes: 0

Kernel James
Kernel James

Reputation: 4074

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.

Upvotes: 1

Related Questions