[Design Pattern] Observer Pattern

concept

The Observer pattern is also known as the Publish/Subscribe pattern.
Defines a one-to-many dependency between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated. The Observer pattern is a behavioral pattern.

Components of the Observer Pattern

Abstract observer role: define an interface for all concrete observers that update themselves when notified by the subject.

Concrete Observer Role: This role implements the update interface required by the abstract observer role in order to reconcile its own state with the state of the subject. Usually implemented with a subclass. A concrete observer role can hold a reference to a concrete subject role if desired.

Abstract subject roles: Keep all references to observer objects in a collection, each abstract subject role can have any number of observers. Abstract topics provide an interface to add and remove observer roles. It is generally implemented with an abstract class and interface.

Topic-specific roles: Notify all registered observers when the internal state of a specific topic changes. Concrete subject roles are usually implemented with a subclass.

application

Application examples: 1. During the auction, the auctioneer observes the highest bid, and then notifies other bidders to bid.

Code

Abstract subject roles omitted

Observed

That is, a specific theme role.

//被观察者
public class Subject {

    private List<Observer> observers = new ArrayList<>();    //状态改变

    public void setMsg(String msg) {
        notifyAll(msg);
    }
    //增加订阅者
    public void addAttach(Observer observer) {
        observers.add(observer);
    }
    //通知所有订阅的观察者
    private void notifyAll(String msg) {
        for (Observer observer : observers) {
            observer.update(msg);
        }
    }
}

observer

abstract observer role

public abstract class Observer {
    public abstract void update(String msg);
}

first observer

specific observer role

public class F_Observer extends Observer{
    @Override
    public void update(String msg) {
        System.out.println(F_Observer.class.getName() + " : " + msg);
    }
}

second observer

specific observer role

public class S_Observer extends Observer {
    @Override
    public void update(String msg) {
        System.out.println(S_Observer.class.getName() + " : " + msg);
    }
}

Execute Demo

public class Main {

    public static void main(String[] args) {
        F_Observer f_observer = new F_Observer();//第一个观察者
        S_Observer s_observer = new S_Observer();
        T_Observer t_observer = new T_Observer();

        //被观察者
        Subject subject = new Subject();
        //增加订阅者
        subject.addAttach(f_observer);
        subject.addAttach(s_observer);
        subject.addAttach(t_observer);
        //被观察者被修改,通知所有订阅者
        subject.setMsg("被观察了...");
    }
}

Results of the

com.learn.model.ObserverModel.F_Observer : 被观察了...
com.learn.model.ObserverModel.S_Observer : 被观察了...

References

http://www.runoob.com/design-pattern/observer-pattern.html
http://www.cnblogs.com/mengdd/archive/2013/02/07/2908929.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324650906&siteId=291194637