zoukankan      html  css  js  c++  java
  • 观察者模式

    观察者模式 -  在对象之间定义一对多的依赖,这样一来,当一个对象改变状态,依赖他的对象都会收到通知,并更新

    /** * @description 观察者 更新消息 */ public interface Observer { /** * 功能描述: 更新 * @author a * @date 14:53 2020/11/23 * @param temp * @param humidity * @param pressure */ void update(float temp, float humidity, float pressure); }

      

    package com.design.demo5;
    
    /**
     * @description 主题
     */
    public interface Subject {
    
        /**
         * 功能描述: 添加观察者
         * @param observer 观察者对象
         */
        void registerObserver(Observer observer);
    
    
        /**
         * 功能描述: 删除观察者
         * @param observer 观察者对象
         */
        void removeObserver(Observer observer);
    
        /**
         * 功能描述: 通知观察者
         */
        void notifyObserver();
    }
    

      

    package com.design.demo5;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @description 天气数据 改变气象数据 通知观察者
     */
    public class WeatherData implements Subject {
    
        private List<Observer> observers;
        private float temperature;
        private float humidity;
        private float pressure;
    
        public WeatherData() {
            this.observers = new ArrayList<>();
        }
    
        //改变气象数据
        public void setMeasurements(float temperature, float humidity, float pressure) {
            // 更改气象数据
            this.temperature = temperature;
            this.humidity = humidity;
            this.pressure = pressure;
            // 通知观察者
            measurementsChanged();
        }
    
        private void measurementsChanged() {
            notifyObserver();
        }
    
        @Override
        public void registerObserver(Observer observer) {
            observers.add(observer);
        }
    
        @Override
        public void removeObserver(Observer observer) {
            int i = observers.indexOf(observer);
            if (i >= 0) {
                observers.remove(i);
            }
        }
    
        @Override
        public void notifyObserver() {
            for (Observer observer : observers) {
                observer.update(temperature, humidity, pressure);
            }
        }
    
        public float getTemperature() {
            return temperature;
        }
    
        public void setTemperature(float temperature) {
            this.temperature = temperature;
        }
    
        public float getHumidity() {
            return humidity;
        }
    
        public void setHumidity(float humidity) {
            this.humidity = humidity;
        }
    
        public float getPressure() {
            return pressure;
        }
    
        public void setPressure(float pressure) {
            this.pressure = pressure;
        }
    }
    

      

    package com.design.demo5;
    
    /**
     * @description 观察者对象
     */
    public class ObserverTest01 implements Observer {
    
        private float temperature;
        private float humidity;
        private float pressure;
        private WeatherData weatherData;
    
        public ObserverTest01(WeatherData weatherData) {
            this.weatherData = weatherData;
            //订阅
            weatherData.registerObserver(this);
        }
        @Override
        public void update(float temp, float humidity, float pressure) {
            this.temperature = temp;
            this.humidity = humidity;
            this.pressure = pressure;
            display();
        }
        public void display() {
            System.out.println("[push] 当前温度01:" + temperature + " 当前湿度01: " + humidity + " 当前压力01:" + pressure);
        }
    }
    public class ObserverTest02 implements Observer {
    
        private float temperature;
        private float humidity;
        private float pressure;
        private WeatherData weatherData;
    
        public ObserverTest02(WeatherData weatherData) {
            this.weatherData = weatherData;
            //订阅
            weatherData.registerObserver(this);
        }
        @Override
        public void update(float temp, float humidity, float pressure) {
            this.temperature = temp;
            this.humidity = humidity;
            this.pressure = pressure;
            display();
        }
        public void display() {
            System.out.println("[push] 当前温度02:" + temperature + " 当前湿度02: " + humidity + " 当前压力02:" + pressure);
        }
    }
    

      

    public class Test {
        public static void main(String[] args) {
            WeatherData weatherData = new WeatherData();
            ObserverTest01 observerTest01 = new ObserverTest01(weatherData);
            ObserverTest02 observerTest02 = new ObserverTest02(weatherData);
            weatherData.setMeasurements(36, 56, 99);
        }
    }
    
    //[push] 当前温度01:36.0 当前湿度01: 56.0 当前压力01:99.0
    //[push] 当前温度02:36.0 当前湿度02: 56.0 当前压力02:99.0
    

      

  • 相关阅读:
    asp.net前台绑定数据和后台绑定数据什么区别
    一个页面多个input 按钮 如何回车控制
    (转)Asp.net中Application Session Cookie ViewState Cache Hidden 区别
    url传值IE6浏览器传值后台读取为乱码
    checkbox实现单选多选
    webconfig和appconfig中出现特殊字符如何处理
    WINCE上遇到空间不足问题
    MessageBox知多少

    for循环之删除注意细节
  • 原文地址:https://www.cnblogs.com/zhaoatian/p/14024839.html
Copyright © 2011-2022 走看看