I was once asked in an interview, “what is the difference between the Observer pattern and Pub-Sub pattern?” I immediately figured that Pub-Sub means ‘Publisher-Subscriber’ and I then vividly recalled from the book “Head first Design Pattern”:
Publishers + Subscribers = Observer Pattern
“I got it, I got it. You can’t trick me Mr.”- I thought.
I replied with a winning smile, “They are same”.
But the interviewer smiled back like he got me and said, “No they are not.”
And I was like-
So, what did I miss? How did it go wrong?
After I came back home, I decided to find the answer by googling. This post is about the excerpt I learned from that query.
Before delving into the difference, let’s first talk about the ‘Observer’ and ‘Pub-Sub’ pattern a bit.
Observer Design Pattern:
I think a lot of people will agree with me that the ‘Observer’ Design Pattern is one of the easiest design patterns of them all. I mean, unlike most of the other design patterns, you can at least ‘Feel’ what is the main concept of the Observer design pattern by the first read.
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.
See, even the Wikipedia definition is not that hard, right? If you are still struggling, let’s explain it in layman’s term.
Let’s assume that you are searching for a job as a software engineer and very interested in a company named ‘Banana Inc.’. So, you contacted their Hiring Manager and gave him your contact number. He ensured you that if there is any vacancy they will let you know. And there are several other candidates interested too, like you. They will let all of the candidates know about the vacancy and maybe if you respond then they will conduct an interview. So, how is this scenario related to ‘Observer’ design pattern? Here, the company ‘Banana Inc.’ is the Subject which is maintaining a list of all the Observers (candidates like you) and will notify the observers for a certain event ‘vacancy’. Ain’t it easy, mate?
So, if you ever need to implement this scenario in some software or application you can follow this process and can say that you have implemented ‘Observer Design Pattern’. (I’m not bloating my article by showing any code example, because there are tons of example available at the mighty Internet)
Pub-Sub(Publisher-Subscriber) Design Pattern:
Yes, the Subject in the Observer Pattern is like a Publisher and the Observer can totally be related to a Subscriber and Yes, the Subject notifies the Observers like how a Publisher generally notify his subscribers. That’s why most of the Design Pattern books or articles use ‘Publisher-Subscriber’ notion to explain Observer Design Pattern. But there is another popular Pattern called ‘Publisher-Subscriber’ and it is conceptually very similar to the Observer pattern. The major difference between the (real) ‘Publisher-Subscriber’ pattern and the ‘Observer’ pattern is this:
Oops! sorry, not that one. This one:
In ‘Publisher-Subscriber’ pattern, senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers.
This means that the publisher and subscriber don’t know about the existence of one another. There is a third component, called broker or message broker or event bus, which is known by both the publisher and subscriber, which filters all incoming messages and distributes them accordingly. In other words, pub-sub is a pattern used to communicate messages between different system components without these components knowing anything about each other’s identity. how does the broker filter all the messages? Actually, there are several processes for message filtering. Most popular methods are: Topic-based and Content-based. Well, not going to further on that road, if you are interested, Wikipedia explained it well.
So, in a nutshell, the major difference between these two patterns can be shown as this:
Let’s list out the differences as a quick Summary:
- In the Observer pattern, the Observers are aware of the Subject, also the Subject maintains a record of the Observers. Whereas, in Publisher/Subscriber, publishers and subscribers don’t need to know each other. They simply communicate with the help of message queues or broker.
- In Publisher/Subscriber pattern, components are loosely coupled as opposed to Observer pattern.
- Observer pattern is mostly implemented in a synchronous way, i.e. the Subject calls the appropriate method of all its observers when some event occurs. The Publisher/Subscriber pattern is mostly implemented in an asynchronous way (using message queue).
- Observer pattern needs to be implemented in a single application address space. On the other hand, the Publisher/Subscriber pattern is more of a cross-application pattern.
Despite the differences between these patterns, some might say that Publisher-Subscriber pattern is a variation of Observer pattern because of the conceptual similarity between them. And it won’t be wrong at all. Don’t need to take the differences religiously. They are similar, aren’t they?
Well, that’s all for now. Hope you get the idea. Thanks for reading the article. Please let me know if there is any mistake or any modification needed. Thanks in advance.