Java support for the observer pattern

Built-in Java classes observer pattern associated with the interface, and are based Observable Observer interface, which realizes the function of Observable Observer deletions and notification, and provides the interface updates Observer

public interface Observer {
    void update(Observable o, Object arg);
}

Observable class notification follows

    public void notifyObservers(Object arg) {
        Object[] arrLocal;
        synchronized (this) {
            if (!changed)
                return;
            arrLocal = obs.toArray();
            clearChanged();
        }

        for (int i = arrLocal.length-1; i>=0; i--)
            ((Observer)arrLocal[i]).update(this, arg);
    }

The viewer interface conversion Observable stored as local variables, in order to reduce the lock granularity

Guess you like

Origin www.cnblogs.com/yytxdy/p/12227962.html