I was once asked in an interview, “What’s the difference between the Observer model and the publish-subscribe model?” I quickly recalled the book “Head First Design Patterns” :
Publish + subscribe = Observer mode
“I know, I know, don’t lie to me.”
I smiled and replied, “There is no difference. They are the same.”
But the interviewer laughed, “No, they’re different.”
The look on my face:
So am I wrong? Then I went home and Google the answer. This article is my summary after Googling.
Before diving into the differences, let’s discuss the “observer model” and the “publish and subscribe model.”
Observer Design Pattern:
I think most people would agree that the Observer model is the best place to start, because you literally know what it’s mainly about.
The observer pattern is an object in software design that maintains a list of dependencies and automatically notifies them when any state changes.
See, even by Wikipedia’s definition, it’s not that hard, right? If you don’t know, let me explain in plain English.
Let’s say you’re looking for a job as a software engineer and are interested in “Banana Company.” So you contacted their HR and gave him your contact number. He promised to let you know if there were any job openings. There are several other candidates who are equally interested in you. So the job opening will be known, and if you respond to their call, they will contact you for an interview.
So what does this have to do with the observer model? The “banana company” here is the Subject, designed to maintain Observers like you, and notify ** Observers for certain events, such as job openings.
Isn’t it easy? ?
So, if you want to implement observer mode in your software or application, you can follow this process. (I don’t want to write any code in my article because there are countless examples on the web.)
Publish and subscribe design patterns
A Subject in the Observer mode is like a Publisher, and the Observer is completely associated with the Subscriber. Subject notifies the observer just as a publisher notifies its subscribers. This is why many books and articles use the publish-subscribe concept to explain the observer design pattern. But there is another popular pattern called the publish-subscribe design pattern. The concept is very similar to the observer model. The biggest differences are:
In the publish-subscribe model, the sender of a message is called a publisher, and the message is not sent directly to a specific recipient, called a subscriber.
This means that publishers and subscribers are unaware of each other’s existence. You need a third-party component, called a message mediator, that connects subscribers and publishers, and that filters and allocates all incoming messages. In other words, the publish-subscribe pattern is used to handle the exchange of information between different system components, even if they are unaware of each other’s existence.
So how do you filter messages? There are actually several processes, the most popular being topic-based and content-based. Okay, that’s it. If you’re interested, you can go to Wikipedia.
Publish-subscribe model (image: MSDN blog)
So, the most important differences between the two modes are shown below:
Image credit: Developers – Club
Do you feel it?
Let’s quickly summarize the differences:
-
In the observer mode, the observer is aware of the Subject, and the Subject keeps a record of the observer. However, in the publish-subscribe model, publishers and subscribers are unaware of each other’s existence. They communicate only through the message broker.
-
In the publish-subscribe pattern, components are loosely coupled, as opposed to the observer pattern.
-
The observer pattern is synchronous most of the time, such that when an event is triggered, the Subject calls the observer’s methods. The publish-subscribe pattern is asynchronous most of the time (using message queues).
-
The observer pattern needs to be implemented in a single application address space, whereas publish-subscribe is more like the cross-application pattern.
Despite their differences, some might say the publish-subscribe model is a variation of the observer model because they are conceptually similar.
Ok, that’s all I want to say, I hope you get it, thank you for reading, if you find any mistakes or need to modify, please leave a comment below, thank you!
The original address: https://hackernoon.com/observer-vs-pub-sub-pattern-50d3b27f838c translators: MiaoYu