观察者模式

作者: RickGe | 来源:发表于2017-02-13 16:47 被阅读13次

    01 定义

    观察者模式:定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。

    02 情景

    观察者A和观察者B都订阅了天气预报(被观察者),当天气预报发生变化时,观察者A和观察者B都能收到通知。

    03 UML

    Observer Pattern.png

    04 Interface

    // 被观察者
    public interface Observable {
        void addObserver(Observer o);
        void deleteObserver(Observer o);
        void notifyObservers();
    }
    
    // 观察者
    public interface Observer {
        // 更新天气
        void update(int temperature);
    }
    

    05 Class

    // 天气预报(被观察者)
    public class WeatherObservable implements Observable {
        private ArrayList<Observer> observers;
        private int temperature;
    
        public WeatherObservable(){
            observers = new ArrayList<>();
        }
    
        public void SetTemperature(int temperature){
            this.temperature = temperature;
            temperatureChanged();
        }
        private void temperatureChanged(){
            notifyObservers();
        }
        @Override
        public void addObserver(Observer o) {
            if(o != null && !observers.contains(o)){
                observers.add(o);
            }
        }
        @Override
        public void deleteObserver(Observer o) {
            observers.remove(o);
        }
        @Override
        public void notifyObservers() {
            for(Observer observer : observers){
                observer.update(temperature);
            }
        }
    }
    
    // 观察者A
    public class ObserverA implements Observer {
        public void subscribe(Observable observable){
            observable.addObserver(this);
        }
        public void unSubscribe(Observable observable){
            observable.deleteObserver(this);
        }
        @Override
        public void update(int temperature) {
            display(temperature);
        }
        public void display(int temperature){
            System.out.println("This is from ObserverA : " + temperature);
        }
    }
    
    // 观察者B
    public class ObserverB implements Observer {
        public void subscribe(Observable observable){
            observable.addObserver(this);
        }
        public void unSubscribe(Observable observable){
            observable.deleteObserver(this);
        }
        @Override
        public void update(int temperature) {
            display(temperature);
        }
        public void display(int temperature){
            System.out.println("This is from ObserverB : " + temperature);
        }
    }
    

    06 测试

    WeatherObservable weatherObservable = new WeatherObservable();
    
    ObserverA observerA = new ObserverA();
    observerA.subscribe(weatherObservable);
    ObserverB observerB = new ObserverB();
    observerB.subscribe(weatherObservable);
    
    weatherObservable.SetTemperature(10);
    // 取消订阅
    observerA.unSubscribe(weatherObservable);
    weatherObservable.SetTemperature(20);
    
    TestResult.png

    相关文章

      网友评论

        本文标题:观察者模式

        本文链接:https://www.haomeiwen.com/subject/jahtwttx.html