"Design Model" observer mode

Observer Pattern

Observer model defines dependencies between many objects, so that, when an object changes state, all of its dependents will be notified and updated automatically

The observer pattern on two roles, one observer, the second is the observed (theme), it can also be considered subscribers and publishers.

Received notification that an observer.

If the observer and the observed words have been confused, you can publish a newspaper subscription to cover. Customers subscribe to the newspaper, the newspaper income people (income notice), so the customer is an observer, it is to be an observer of the newspaper.

use

/**
 * 抽象类
 * @author aodeng-低调小熊猫
 * @since 19-7-15
 */
public abstract class Customer {
    /**
    * <p>
    * 定义一个抽象方法
    * </p>
    * @author aodeng
    * @since 19-7-15
    */
    public abstract void update();

}复制代码

/**
 * 收到通知的观察者客户A
 * @author aodeng-低调小熊猫
 * @since 19-7-15
 */
public class CustomerA extends Customer {
    @Override
    public void update() {
        System.out.println("我是客户A,我收到报纸了!");
    }
}复制代码

/**
 * 收到通知的观察者客户B
 * @author aodeng-低调小熊猫
 * @since 19-7-15
 */
public class CustomerB extends Customer {
    @Override
    public void update() {
        System.out.println("我是客户B,我收到报纸了");
    }
}复制代码

/**
 * 被观察者 报社
 * @author aodeng-低调小熊猫
 * @since 19-7-15
 */
public class NewsOffice {
    private List<Customer> customerList=new ArrayList<>();

    public void add(Customer customer){
        this.customerList.add(customer);
    }

    /** 
    * <p>
    * 模拟新报纸来了
    * </p> 
    * @author aodeng
    * @since 19-7-15
    */
    public void newspaperCome(){
        this.notifyAllObservers();
    }

    public void notifyAllObservers(){
        for (Customer customer : customerList) {
            customer.update();
        }
    }
}复制代码

test

    public static void main(String[] args) {
        System.out.println("Hello World!");
        NewsOffice newsOffice=new NewsOffice();
        newsOffice.add(new CustomerA());
        newsOffice.add(new CustomerB());
        newsOffice.newspaperCome();

    }
    输出:
    Hello World!
    我是客户A,我收到报纸了!
    我是客户B,我收到报纸了复制代码

strengthen

/**
 * 定义被观察者接口
 * @author aodeng-低调小熊猫
 * @since 19-7-15
 */
public interface ISubject {
    public void registerObserver(Customer customer);
    public void removeObserver(Customer customer);
    public void notifyObservers();

}复制代码
/**被观察者 报社 加强版
 * @author aodeng-低调小熊猫
 * @since 19-7-15
 */
public class NewsOfficeNiu implements ISubject {

    private List<Customer> customerList = new ArrayList<>();

    @Override
    public void registerObserver(Customer customer) {
        this.customerList.add(customer);
    }

    @Override
    public void removeObserver(Customer customer) {
        customerList.remove(customer);
    }

    @Override
    public void notifyObservers() {
        for (Customer customer : customerList) {
            customer.update();
        }
    }

    //模拟报纸来了
    public void newspaperCome(){
        this.notifyObservers();
    }
}复制代码

test

        System.out.println("加强版========");
        ISubject iSubject=new NewsOfficeNiu();
        Customer customera=new CustomerA();
        iSubject.registerObserver(customera);
        iSubject.removeObserver(customera);
        iSubject.registerObserver(new CustomerB());
        ((NewsOfficeNiu) iSubject).newspaperCome();
        
       输出:
       加强版========
       我是客户B,我收到报纸了复制代码

Java provides the observer mode

Java has been provided to the Observer pattern we need categories: Observer class and the Subject class, but do not call Subject Subject in Java, and called Observable.

copy a demo

//demo
public class NewsOffice2 extends Observable {

    /**
     * 模拟报纸来了
     */
    public void newspaperCome(){
        this.setChanged();
        this.notifyObservers();
    }
}
public class CustomerC implements Observer {

    @Override
    public void update(Observable o, Object arg) {
        System.out.println("我是客户C,我收到消息啦");
    }
}

//测试
Observableoffice = new NewsOffice2();
Observer observer = new CustomerC();

office.addObserver(observer);

((NewsOffice2) office).newspaperCome();复制代码

to sum up

comes into consideration java thread safety, but to write a lot more convenient time, pros and cons

1. Advantages abstract coupling between the observer and the observed, has a set of trigger mechanism, the observed objects without knowing who notice that, as long as you can meet the viewer interface.

2. The only disadvantage of the observer is aware of a change in the observer, and can not know how the change occurred, for example, to modify the name field or the other, observers do not know. If you have a lot of viewers, one notification time-consuming.

Source

Source address: https: //github.com/java-aodeng/hope

Links:

This paper consists of a low-key multiple operating panda article published! No welcome public attention: a low-key red panda

Guess you like

Origin juejin.im/post/5dbbf34b51882522e83f4bbf