The original address: zhuanlan.zhihu.com/p/51357583 author: willow
In one interview, the interviewer asked,
What’s the difference between the observer model and the publish-subscribe model?
Immediately, something from Head First Design Patterns came to mind:
Publishers + Subscribers = Observer Pattern
“Hum, I know the answer, brother dei!” I was so cold inside.
“They’re the same.” I pretended to be calm and smiled at the corners of my mouth as if the interviewer would offer me an offer the next second.
The interviewer laughed too. “No, they’re different.”
And I was like,
So, why am I wrong? What is the difference between the Observer pattern and the publish-subscribe pattern?
Observer model
The so-called observer mode is only loosely coupled.
Using the weather station in Head First design Mode as an example, the changed() method is called whenever the weather measurements are updated, so we can update the data on the weather instruments, such as temperature, pressure, and so on, within the Changed () method.
The problem with this is that if we want to update the changed() method later when it is called with more information, such as humidity, we have to change the code for the changed() method, which is the downside of tight coupling.
How do you solve it? Use the observer pattern, interface oriented programming to achieve loose coupling.
In Observer mode, the instance object on which the changed() method is located is the Subject, or Observable. It simply maintains a collection of observers that implement the same interface. The Subject only needs to know that Notifying the Observer which unified method needs to be invoked:
I’m not going to post the code here, but there’s a lot of information on the web.
Publish and subscribe model
Probably many people are just like me, thinking that Publisher in the subscription mode is the Subject in the Observer mode, and Subscriber is the Observer. When Publisher changes, take the initiative to inform Subscriber.
It’s not.
In the publish-subscribe model, the publisher does not directly notify the subscriber, in other words, the publisher and the subscriber do not know each other.
Strangers? How do they communicate with each other?
The answer is through a third party, a Broker, in the message queue.
The publisher simply tells the Broker that the topic I want to send is AAA;
The subscriber simply tells the Broker that I want to subscribe to messages whose topic is AAA;
Thus, when the Broker receives a message from a publisher whose topic is AAA, it pushes the message to subscribers whose topic is AAA. Of course, there is also the possibility that the subscribers themselves come to pull, see the concrete implementation.
In other words, publishers and subscribers are not loosely coupled, but completely decoupled in the publish-subscribe model.
Here’s a simple diagram to show the difference between the two modes:
conclusion
On the face of it:
- In observer mode, there are only two roles – observer + observed
- In the publish-subscribe model, there are not only publishers and subscribers, but also a Broker that is often overlooked
To go deeper:
- The observer and the observed are loosely coupled
- Publishers and subscribers, there is no coupling at all
From the perspective of use:
- Observer mode, mostly used within a single application
- The publish-subscribe pattern is more of a cross-application pattern, such as messaging middleware
Finally, all my words are parodies of this article: Observer vs pub-sub Pattern