Photo by on Ben White Unsplash I was once asked in an interview, I immediately figured that Pub-Sub means and I then vividly recalled from the book “Head first Design Pattern”: “what is the difference between the Observer pattern and Pub-Sub pattern?” ‘Publisher-Subscriber’ Publishers + Subscribers = Observer Pattern - I thought. “I got it, I got it. You can’t trick me Mr.” I am adding this irrelevant GIF, like everybody do I replied with a winning smile, . “They are same” But the interviewer smiled back like he got me and said, And I was like- “No they are not.” Yes, I’ve done it again 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 is a software design pattern in which an object, called the , maintains a list of its dependents, called , and notifies them automatically of any state changes, usually by calling one of their methods. observer pattern subject observers See, even the is not that hard, right? If you are still struggling, let’s explain it in layman’s term. Wikipedia definition 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 which is maintaining a list of all the (candidates like you) and will the observers for a certain ‘vacancy’. Ain’t it easy, mate? Subject Observers notify event Observer design pattern (A bird’s eye view :P) 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 in the Observer Pattern is like a and the can totally be related to a 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 and it is conceptually very similar to the Observer pattern. The major difference between the (real) pattern and the pattern is this: Subject Publisher Observer Subscriber ‘Publisher-Subscriber’ ‘Publisher-Subscriber’ ‘Observer’ Oops! sorry, not that one. This one: In ‘Publisher-Subscriber’ pattern, senders of messages, called , do not program the messages to be sent directly to specific receivers, called publishers subscribers. This means that the publisher and subscriber don’t know about the existence of one another. There is a third component, called , 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: and . Well, not going to further on that road, if you are interested, . broker or message broker or event bus Topic-based Content-based Wikipedia explained it well Pub-Sub Pattern (image credit: ) MSDN blog So, in a nutshell, the major difference between these two patterns can be shown as this: Image source: developers-club Makes sense? Let’s list out the differences as a quick Summary: In the pattern, the O** **. Whereas, in , publishers and subscribers . They simply communicate with the help of message queues or broker. Observer bservers are aware of the Subject, also the Subject maintains a record of the Observers Publisher/Subscriber don’t need to know each other In pattern, components are loosely coupled as opposed to pattern. Publisher/Subscriber Observer pattern is mostly implemented in a way, i.e. the Subject calls the appropriate method of all its observers when some event occurs. The pattern is mostly implemented in an way (using message queue). Observer synchronous Publisher/Subscriber asynchronous pattern needs to be implemented in a single application address space. On the other hand, the pattern is more of a cross-application pattern. Observer Publisher/Subscriber Despite the differences between these patterns, some might say that pattern is a variation of 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? Publisher-Subscriber Observer 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.
Share Your Thoughts