Observer Model-Behavioral

Creation type

1. Singleton design pattern
2. Factory design pattern
3. Builder design pattern
4. Prototype design pattern

Structure type

5. Agency design pattern
6, bridging design pattern
7, decoration design pattern
8, adapter design pattern
9, appearance design pattern
10, flyweight design pattern
11, combination design pattern

Behavioral

12. Template design mode

Preface

The 23 classic design patterns are often divided into three categories: creation, structure, and behavior. I have learned the creation and structure types before, and start learning behavioral design patterns from this article. As mentioned earlier, the creational design pattern mainly solves the problem of "object creation", the structural design pattern mainly solves the problem of "combination or assembly of classes or objects", and the behavioral design pattern mainly solves the "interaction between classes or objects." "problem.

Introduction

Observer Design Pattern, also known as Publish-Subscribe Design Pattern, defines a one-to-many dependency relationship between objects, so that whenever an object changes state, all objects that depend on it Will be notified and updated automatically;

UML graphics

Subject: Abstract subject, which is also the role of Observable. Each subject can have any number of observers. Abstract subjects provide interfaces to add and delete observer objects;

ConcreteSubject: specific subject, when the internal state of the specific subject changes, a notification is sent to all registered observers, the specific subject is also called the concrete observer (Concrete Observable) role;

Observer: abstract observer, this role is the interface of the observer, it defines an update interface, so that it can update itself when the subject is notified of changes;

ConcreteObserver: Implement concretely, implement the update interface defined by the abstract observer role, so as to update its own state when the state of the subject changes;

Template code implementation

Subject:

public interface Subject {
    void registerObserver(Observer observer); 
    void removeObserver(Observer observer); 
    void notifyObservers(Message message);
}

Observer


public interface Observer { 
    void update(Message message);
}

ConcreteSubject 

public class ConcreteSubject implements Subject {  
    private List<Observer> observers = new ArrayList<Observer>();  
    @Override  
    public void registerObserver(Observer observer) {    
        observers.add(observer);  
    }  
    @Override 
    public void removeObserver(Observer observer) {  
          observers.remove(observer); 
    }  
    @Override 
    public void notifyObservers(Message message) {    
        for (Observer observer : observers) {     
             observer.update(message);    
        }  
    }
}

ConcreteObserver  

public class ConcreteObserverOne implements Observer {  
    @Override  
    public void update(Message message) {    
    //TODO: 获取消息通知,执行自己的逻辑...    
        System.out.println("ConcreteObserverOne is notified.");  
    }
}

public class ConcreteObserverTwo implements Observer {  
    @Override  
    public void update(Message message) {    
        //TODO: 获取消息通知,执行自己的逻辑...    
        System.out.println("ConcreteObserverTwo is notified.");  
    }
}

use:

public class Demo {  
    public static void main(String[] args) {    
        ConcreteSubject subject = new ConcreteSubject();    
        subject.registerObserver(new ConcreteObserverOne());   
        subject.registerObserver(new ConcreteObserverTwo());   
        subject.notifyObservers(new Message()); 
    }
}

The above code can be regarded as the "template code" of the observer mode, which can only reflect the general design ideas. In real software development, there is no need to copy the above template code.

JavaAPI implements observer mode

Observable and Observer types are also built into the JDK in Java, which shows the importance of the observer pattern; Observable is the role of abstract subject (Subject), and Observer is the abstract observer role;

ConcreteSubject 

public class DouYin extends Observable {
    public void postNewPublication(String data) {
        setChanged();
        notifyObservers(data);
    }
}

ConcreteObserver  

public class User implements Observer {
    private static String TAG = char.class.getSimpleName();

    private String name;

    public User(String name) {
        this.name = name;
    }

    @Override
    public void update(Observable o, Object arg) {
        System.out.println("Hi" + name + "你关注的 更新了,内容:" + arg);
    }
}

use

public class Test {
    public static void main(String[] args) {
        //观察者
        User coder = new User("张三");

        //被观察者
        DouYin frontier = new DouYin();

        frontier.addObserver(coder);
        frontier.postNewPublication("发布了新视频");
    }
}

The above implementations are all synchronous blocking methods. The observer and the observer code are executed in the same thread, and the observer is blocked until all the observer codes are executed before executing the subsequent code. How to achieve asynchronous and non-blocking, in each update () function, create a new thread to execute code. This is the simplest way; but whether it is a synchronous blocking implementation or an asynchronous non-blocking implementation, it is an in-process implementation. How to implement a cross-process observer model? Implement inter-process communication in the update() function;

Guess you like

Origin blog.csdn.net/ezconn/article/details/108564814