Site icon Towards AI

Observer Pattern vs. Pub-Sub Pattern

Author(s): Munish Goyal

Software Engineering, Systems

Don’t get confused with these two similar but different patterns, and know which one to use when.

Photo by José Pablo Domínguez on Unsplash

This difference is important to not just generic Software Engineers, but also to Data Engineers and is the basis for the understanding event-driven architectures for data pipelines.

Let’s look at both of them individually, before we eventually list-out the differences.

Observer Pattern

“The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.” — Wikipedia definition [1]

The observer pattern allows a given object (called the subject) to be monitored by a dynamic group of “observer” objects. Whenever a value on the subject changes, it lets all the observers object know that a change has occurred, by calling their methods update() (say) method. Each observer may be responsible for different tasks whenever the core object changes; the subject doesn’t know or care what those tasks are, and the observer doesn’t typically know or care what other observers are doing.

As an example, let’s create a SubjectMixin which we can mix-in in any Python class to make it easily adhere to the Subject of Observer Pattern.

Screenshot of a piece of Author’s code

Similarly, let’s create a ObserverMixin which we can mix-in in any class to make it easily adhere to Object of Observer Pattern.

Screenshot of a piece of Author’s code

Further, let’s create a Subject named Data and two Observers named HexViewer and DecimalViewer.

Screenshot of a piece of Author’s code

Finally, let’s see the observer pattern in action:

Screenshot of a piece of Author’s code

Publisher-Subscribe (Pub-Sub) Pattern

The publisher-subscriber pattern can be considered as an improvized (asynchronous and loosely-coupled) version of the observer pattern. In the pub-sub pattern, senders of messages (called publishers) do not send messages directly to specific receivers (called subscribers). There is an intermediate component, called broker, (or message broker, event bus), to which data is sent by publisher and from where data is received by subscribers. It filters all incoming messages and distributes them accordingly. The popular methods of message filtering are topic-based and content-based.

This means that the publisher and subscriber don’t know even about the existence of one another, and so are just loosely-coupled.

You might like to check Data Streaming with Apache Kafka to appreciate the beauty of the pub-sub mechanism which helps you in making your architecture horizontally-scalable, fault-tolerant, and allows your data transportation at low-latencies.

Difference between Observer and Pub-Sub Pattern

We now understand what individually both of these patterns are. Let’s list their differences:

References:

[1] Observer Pattern, Wikipedia


Observer Pattern vs. Pub-Sub Pattern was originally published in Towards AI — Multidisciplinary Science Journal on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI

Exit mobile version