观察者模式Demo总结

工作之余,写了两个观察者模式的Demo,做做总结

自定义Observer与Observable

 观察者类:

public interface Observer {
    /**
     * 当被观察者变化时,及时反馈至观察者
     * @param objects
     */
    void action(Object... objects);

}

被观察者类:

public abstract class Observable {

    public Vector obs = new Vector<>();
    
    /**
     * 注册观察者对象
     * @param t
     */
    public void registerObserver(T t) {
        isNull(t);
        obs.addElement(t);
    }

    /**
     * 注销观察者对象
     * @param t
     */
    public void unRegisterObserver(T t){
        isNull(t);
        obs.removeElement(t);
    }

    /**
     * 判断传入的观察者对象是否为空
     * @param t
     */
    private void isNull(T t) {
        if (t == null)
            throw new NullPointerException();
    }

    /**
     * 通知观察者做出行为改变
     *
     * @param objects
     */
    public abstract void notifyObservers(Object... objects);
}

创建被观察者类:继承Observable

public class AlarmObservable extends Observable {
    @Override
    public void notifyObservers(Object... objects) {
        for (Observer observer : obs) {
            observer.action(objects);
        }
    }
}

创建观察者类:实现Observer

观察者类1:

public class XBObserver implements Observer {

    @Override
    public void action(Object... objects) {
        if (objects.length > 0 && objects[0] != null) {
            LogUtils.e(objects[0].toString() + "XBObserver ");
        }
    }
}

观察者类2:

public class XZObserver implements Observer {
    @Override
    public void action(Object... objects) {
        if (objects.length > 0 && objects[0] != null) {
            LogUtils.e(objects[0].toString() + "XZObserver ");
        }
    }
}

Activity:

/**
     * 按钮点击事件
     * @param view
     */
    public void alarmClick(View view) {
        alarmObservable = new AlarmObservable();
        XBObserver a = new XBObserver();
        XZObserver b = new XZObserver();
        alarmObservable.registerObserver(a);
        alarmObservable.registerObserver(b);
        alarmObservable.notifyObservers("产生变化----");
        alarmObservable.unRegisterObserver(a);
        alarmObservable.notifyObservers("产生变化----");
    }

日志:

实现java.util.Observer与继承java.util.Observable

 被观察者类:

public class JavaUtilObservable extends Observable {
    
    public synchronized void changed(Object arg) {
        setChanged();
        notifyObservers(arg);  //只有在setChange()被调用后,notifyObservers()才会去调用update(),否则什么都不干。
    }
}

两个观察者类:

观察者类1:

public class JavaUtilObserver implements Observer {
    public JavaUtilObserver(JavaUtilObservable ob) {
        super();
        ob.addObserver(this);
    }

    @Override
    public void update(Observable o, Object arg) {
        LogUtils.e(arg+"JavaUtilObserver");
    }
}

观察者类2:

public class JavaUtilObserver2 implements Observer {
    public JavaUtilObserver2(JavaUtilObservable ob) {
        super();
        ob.addObserver(this);
    }

    @Override
    public void update(Observable o, Object arg) {
        LogUtils.e(arg+"JavaUtilObserver2");
    }

}

Activity:

public void java_util_alarmClick(View view) {
        JavaUtilObservable observable = new JavaUtilObservable();
        JavaUtilObserver a = new JavaUtilObserver(observable);
        JavaUtilObserver2 b = new JavaUtilObserver2(observable);
        observable.changed("产生变化----");
        observable.deleteObserver(b);
        observable.changed("产生变化----");
    }

日志:

你可能感兴趣的:(观察者模式Demo总结)