[design pattern] the design pattern in Hamburg — observer pattern

[design pattern] the design pattern in Hamburg -- observer pattern

Scene bring in

How can it be? You can't wait until the official account is public.

Why do we pay attention to the official account what we can do?

We all know that after the official account is given, you can see the new trend in the subscription message when the official account is released dynamically, as shown in the following figure.

The official account is based on the publish subscribe mode. What is the connection between this and the observer pattern we are going to talk about today?

Publisher subscriber model and observer model

Observer mode (observer)

Although the observer mode is also called "publish subscribe" mode, I think this publish subscribe mode is different from the publisher subscriber mode to be talked about next

The chowhound of our official account is that there are many Observer watching the McDonald's public address (Subject).

Observer and subject are abstract descriptions of observer mode, and their class diagrams are similar. Observer is an abstract class or interface, and the real observer is below. Subject is not designed to be abstract here. You can also abstract the subject and give a specific implementation class

Now let's look at the definition of observer mode by referring to the class diagram. Multiple observers register themselves in the subject. When the subject changes, the notification method will be triggered, and the notification method will call the specific implementation of each specific observer

This is the basic concept of observer model. What are the benefits of observer model?

So what are the shortcomings of the observer?

Publish subscribe mode (publisher subscriber)

In fact, the biggest difference between publish subscribe mode and observer mode is that there is also a middleware between publisher and subscriber to be responsible for scheduling. Publishers do not need to know who the subscriber is, and vice versa

This is a bit like the producer topic consumer in Kafka. Simply draw a picture

Of course, in fact, consumers belong to consumer groups. Consumers in a group subscribe to the same topic. Each consumer receives messages from some partitions of the topic, that is, corresponding to the specific partition in the topic. There is no additional expansion here

As shown in the figure above, producer and consumer don't know each other, but they both know one guy, that is, topic. Producer acts as a publisher, sends information to topic, and consumers listen (subscribe) to topic. Once this topic receives information, it will push the information to the listening consumers

The observer mode mainly triggers the mechanism in a synchronous way, while the publish subscribe mode is more used in the case of asynchrony, with the help of message middleware similar to Kafka to complete the loose coupling between components

So the other two are not equivalent, they can only be said to be somewhat similar

Implementation of observer mode

As long as the thought does not decline, the code is not easy to catch, so understanding the thought is the key

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>