Programmers: I finally know what the observer mode

Author: Thousand absolutely

E-mail: [email protected]

Public number: Thousand absolutely

Title: Design Patterns observer mode (reproduced please indicate the source)

Hello, Tell me what you gentlemen, thousands never updated my back, a very flexible thing recently happened, that I absolutely one thousand public number migrated to a new number above, I am dead, in order to see Tell me what you gentlemen to comment on the article it took me three thousand ocean, a little sad, but you can see the evaluation of the article, but also the value.

In front of a one thousand never introduced the factory model, do not know how to look Tell me what you master it, this article will introduce one thousand must design mode observer pattern, the design pattern here, a thousand absolutely believe it is relatively simple as long as lords Tell me what I carefully read the article if the observer does not know what the pattern is, and how to write an observer mode, then you can spray me (QAQ).

Observer Pattern

definition

First, we refer to the practice content Baidu encyclopedia

The observer pattern (sometimes called publish - subscribe model, model - view mode, source - listener mode or slave mode by) is a software design pattern. In this mode, a target object to manage all observer objects dependent on it, and proactive notification when the state itself changes. This is usually achieved through a method call provided for each viewer. This model is often used to implement event-handling system.

One thousand absolutely understand this definition is: a class manages all observers classes that depend on it, and when it changes the status of these dependencies will take the initiative to give notice of its class.

Understanding or very good understanding, and if you can not understand the words above, thousands of them never will use the example of life to explain to you.

example

If there is a website called endpoint, is a specialized look at the website of the novel, we can read novels on it, one day end, product manager of the updated article, we would like to inform subscribe to readers of this novel, unsubscribe on the land less than inform.

This time one thousand never thought it is not that the observer mode, nothing gave them to me.

Code

Define an interface to the observed

public interface Observerable{
    public void addObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObserver();
}
复制代码

The definition of an observer Interface

public interface Observer{
    public void update(String message);
}
复制代码

The definition of the class implements the interface Observerable

public class Author implements Observerable {
    private List<Observer> list;
    private String name;
    private String bookName;
    public Author(String name,String bookName){
        this.list = new ArrayList<Observer>();
        this.name = name;
        this.bookName = bookName;
    }
    public void addObserver(Observer o) {
        list.add(o);
    }

    public void removeObserver(Observer o) {
        if(list.contains(o)){
            list.remove(o);
        }
    }

  public void notifyObserver() {
        for(Observer observer : list){
            observer.update(this.name + " 更新了 《" + this.bookName + "》 新的一章");
        }
    }
}
复制代码

Readers defined class that implements the Observer interface

public class Reader implements Observer {
    private String name;
    public Reader(String name){
        this.name = name;
    }
    public void update(String message) {
        System.out.println(this.name + " : " +message);
    }
}
复制代码

have a test

public class TestObserver {
    public static void main(String []args){
        Author author = new Author("总管","陈平安讲道理");
        Reader reader1 = new Reader("reader1");
        Reader reader2 = new Reader("reader2");
        Reader reader3 = new Reader("reader3");
        Reader reader4 = new Reader("reader4");
        author.addObserver(reader1);
        author.addObserver(reader2);
        author.addObserver(reader3);
        author.addObserver(reader4);
        author.notifyObserver();
        System.out.println("reader1取消了订阅");
        author.removeObserver(reader1);
        author.notifyObserver();
    }
}
复制代码

Here Insert Picture Description

Explorer update Chen Pingan latest chapter, readers can subscribe to receive four, then the reader a feel Explorer update is too slow to unsubscribe, and the results Explorer and then update, readers will receive a 1.

Tell me gentlemen, the above example is not very easy to understand, the observer pattern is such a simple model. If you can not read the message to me, a thousand doubts must help you.

to sum up

Thanks for watching Tell me what master's.

Next issue, the iterator pattern.

If you have a favorite one thousand absolutely can follow, comment, forwarding it.

We must be concerned about one thousand of the public numbers, one thousand micro-channel search must, come play with me.

Here Insert Picture Description

Guess you like

Origin juejin.im/post/5e6ee8e56fb9a07c944cae86