Publisher-subscriber vs Observer [closed]
Asked Answered
J

0

46

I am trying to make sense of Observer design pattern as the main event dispatching design pattern. The Observer pattern appears to be type or kind of Publish-Subscribe design pattern and I am wondering why there are two, similar looking design patterns and how I should choose between the two.

In both cases informational messages are sent between entities and in both cases entities must register or subscribe in some way in order to receive messages.

The main differences between the two design patterns seems to be:

  • The Observer pattern would seem to have a lower volume of messages. Observers register with the entity they are observing to be notified of a certain event. The Publish-Subscribe pattern seems to involve a one way conversation, a kind of monologue from one entity to another with a larger volume of more varied types of messages.
  • In Observer pattern, the Observers expects a particular kind of event notification where as Publish-Subscribe seems to have a more open message protocol with a wider range of possible message types and message content (a difference between a message triggered by a specific kind of event indicating the event happened versus a more general message protocol with a message containing information that may be other than the event trigger such as a message containing temperature data that was triggered by a periodic timer).
  • Observer is implemented within the boundaries of an application or within a single process. Publish-Subscribe is a cross application communication pattern with messages being exchanged between different processes.
  • There is an architecture difference in that Publish-Subscribe has separate publisher, broker, and subscriber entities while Observer makes use of a combined publisher/broker and subscriber (observers) entities.

One thought that comes to mind is that in a multi-threaded application, the Publish-Subscribe pattern may be used when communicating between multiple threads rather than the Observer pattern. And perhaps the Observer pattern could be used between processes for instance a process registers with another process to be notified should an event happen. An example would be a farm management dashboard application that registers with multiple animal feed bins to be notified if the feed level or weight measured by the bin drops below some threshold.

Are there any other important differences between these two design patterns which would provide criteria for choosing one over the other?

Jeff answered 8/8, 2012 at 3:35 Comment(3)
You said it right on the third point: Publisher-subscriber is a network oriented architectural pattern and Observer is an object-event oriented pattern. They both are used at different Software levels.Gaynellegayner
Your point three is the most important, the Observer Pattern must be implemented within the same address space. The Observer and Subject are coupled. There is no such restriction for Publish-Subscribe which is coupled to a topic, a strong abstraction.Isthmian
Great edit from Richard Chambers, I vote for reopen.Lacteal

© 2022 - 2024 — McMap. All rights reserved.