Observer model of behavior model

1 General Observer pattern, also known as pub...
1 General

Observer pattern, also known as publish subscribe pattern, is a very important behavior pattern. It is widely used in asynchronous programming, which can be said to be the basis of asynchronous programming.

2 observer mode

When we need to pay attention to the state of an object, we can constantly poll to see whether the state changes, which is also known as synchronization. However, this method is inefficient. Before the object state changes, any check is a waste of server resources. Better yet, when an object's state changes, there can be a notification mechanism that tells others that the state has changed. The observer model is the key to realize this mechanism.

In the observer mode, the observer can register to the observed objects that he is interested in. When the state of the observed objects changes or an event is released, the observer's methods are called one by one to inform them. This asynchronous way greatly improves the operation efficiency

In real life, the observer model is also very common. For example, when we buy a product online, we don't need to check whether the express delivery has arrived at the receiving point from time to time. Instead, when we receive the message "the product has been delivered", we just go to get it directly. In this example, when we buy goods, we pay attention to the status of express delivery. When the status changes to "delivered", we will be notified.

3 cases

Take a code case. When we focus on a official account, we can receive the official account's article push. This is actually an example of the typical observer pattern.

public interface Observable<T> { void addSubscriber(Observer observer); void removeSubscriber(Observer observer); void publish(T object); } public class PublicAccount implements Observable<Article> { private String name; private Collection<Observer> subscribers; private Collection<Article> articles; PublicAccount(String name) { this.name = name; subscribers = new ArrayList<>(); articles = new LinkedList<>(); }; [@Override](https://my.oschina.net/u/1162528) public synchronized void addSubscriber(Observer observer) { subscribers.add(observer); } [@Override](https://my.oschina.net/u/1162528) public synchronized void removeSubscriber(Observer observer) { subscribers.remove(observer); } [@Override](https://my.oschina.net/u/1162528) public synchronized void publish(Article article) { articles.add(article); System.out.println(name + " is publishing new article..."); for (Observer subscriber : subscribers) { subscriber.update(article); } } } public interface Observer<T> { void update(T object); } public class ArticleFans implements Observer<Article> { private String name; ArticleFans(String name) { this.name = name; }; [@Override](https://my.oschina.net/u/1162528) public void update(Article article) { System.out.println(name + " got article「" + article.getName() + "」"); } } public class Article { private String name; private String content; Article(String name, String content) { this.name = name; this. content = content; }; public String getName() { return name; } } public class Test { public static void main(String[] args) { PublicAccount publicAccount = new PublicAccount("Jump x Switch"); ArticleFans link = new ArticleFans("Link"); ArticleFans mario = new ArticleFans("Mario"); publicAccount.addSubscriber(link); publicAccount.addSubscriber(mario); Article article = new Article("Pro Controller is on discount!", "......"); publicAccount.publish(article); } }

Output:

Jump x Switch is publishing new article... Link got article「Pro Controller is on discount!」 Mario got article「Pro Controller is on discount!」

Our official account is official account. It is actually a list of observers of the official account. When the public number releases a new article, all the people who are concerned about it will be pushed by the article.

In JDK java.util.Observable and java.util.Observer , is a simple implementation of the observer pattern, but it is rarely used in reality (many applications in SWT). Observer pattern is more common in the framework of event notification model, such as Netty ChannelFuture and GenericFutureListener A combination of; can also be seen in Redis's Pub/Sub , ZooKeeper's Watches......

4 Summary

Observer pattern allows us to be notified when the state of an object changes. It has been widely used in some high-performance frameworks and is the basis of asynchronous programming.

The github address of the example in this paper

10 May 2020, 08:55 | Views: 9165

Add new comment

For adding a comment, please log in
or create account

0 comments