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

    主体接口-》主体实现

    观察者接口-》观察者实现

    地址:

    主体接口:

    package Observer;
    
    /*
     * Create by dapeng on 2018/1/15
     */
    public interface Subject {
        //观察者注册
        void registeObserver(Observer observer);
    
        //移除观察者
        void removeObserver(Observer observer);
    
        //通知观察者
        void notifyObserver();
    }
     

    主体实现:

    package Observer;
    
    import java.awt.geom.FlatteningPathIterator;
    import java.util.ArrayList;
    import java.util.List;
    
    /*
     * Create by dapeng on 2018/1/15
     */
    public class WeatherDataSubject implements  Subject {
        private List<Observer> observers;
    
        private float temperature;//温度
        private float humidity;//湿度
        private float pressure;//气压
        private List<Float> forecastTemperatures;//未来几天的温度
    
        public WeatherDataSubject(){
            this.observers = new ArrayList<Observer>();
        }
    
        @Override
        public void registeObserver(Observer observer) {
            this.observers.add(observer);
        }
    
        @Override
        public void removeObserver(Observer observer) {
            this.observers.remove(observer);
        }
    
        @Override
        public void notifyObserver() {
            for(Observer observer:observers){
                observer.update();
            }
        }
        public void measurementChanged(){
            notifyObserver();
        }
        public void setWeather(float temperature,float humidity,float pressure,List<Float> forecastTemperatures){
            this.temperature = temperature;
            this.humidity = humidity;
            this.pressure = pressure;
            this.forecastTemperatures = forecastTemperatures;
            //改变后的反应
            measurementChanged();
        }
        public float getTemperature() {
            return temperature;
        }
    
        public float getHumidity() {
            return humidity;
        }
    
        public float getPressure() {
            return pressure;
        }
    
        public List<Float> getForecastTemperatures() {
            return forecastTemperatures;
        }
    }

    观察者接口:

    public interface Observer {
        void update();
    }

    功能接口:

    public interface DisplayElement {
        void display();
    }

    观察者1:

    package Observer;
    
    /*
     * Create by dapeng on 2018/1/15
     */
    public class CurrentConditionDisplay implements Observer ,DisplayElement {
        //观察这个天气的变化
        private WeatherDataSubject weatherDataSubject;
    
        private float temperature;
        private float humidity;
        private float pressure;
    
        public  CurrentConditionDisplay(WeatherDataSubject weatherDataSubject){
            this.weatherDataSubject = weatherDataSubject;
            this.weatherDataSubject.registeObserver(this);
        }
        @Override
        public void display() {
            System.out.println("当前温度为:" + this.temperature + "℃");
            System.out.println("当前湿度为:" + this.humidity);
            System.out.println("当前气压为:" + this.pressure);
        }
    
        @Override
        public void update() {
            this.temperature = this.weatherDataSubject.getTemperature();
            this.humidity = this.weatherDataSubject.getHumidity();
            this.pressure = this.weatherDataSubject.getPressure();
            display();
        }
    }

    观察者2:

    package Observer;
    
    import java.util.List;
    
    /*
     * Create by dapeng on 2018/1/15
     */
    public class ForecastDisplay implements Observer , DisplayElement{
        private WeatherDataSubject weatherDataSubject;
        private List<Float> forecastTemperatures;
    
        public ForecastDisplay(WeatherDataSubject weatherDataSubject){
            this.weatherDataSubject = weatherDataSubject;
            weatherDataSubject.registeObserver(this);
        }
    
    
        @Override
        public void update() {
            this.forecastTemperatures = this.weatherDataSubject.getForecastTemperatures();
            display();
        }
    
        @Override
        public void display() {
            int count = 0;
            System.out.print("未来几天的天气 ");
            for(Float f:forecastTemperatures){
                System.out.println("第"+ ++count +"天的温度是"+f);
            }
        }
    }

    主函数:

    package Observer;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /*
     * Create by dapeng on 2018/1/15
     */
    public class Main {
        public static void main(String [] arg){
         WeatherDataSubject weatherDataSubject = new WeatherDataSubject();
         CurrentConditionDisplay currentConditionDisplay = new CurrentConditionDisplay(weatherDataSubject);
        ForecastDisplay forecastDisplay = new ForecastDisplay(weatherDataSubject);
    
        List<Float> f = new ArrayList<Float>();
        f.add(30.2f);
        f.add(30f);
        f.add(49f);
        weatherDataSubject.setWeather(22,3,500,f);
    
    
    
        }
    
    }
  • 相关阅读:
    D
    C
    如何用最简单的方式解释依赖注入?依赖注入是如何实现解耦的?
    二维数组中的查找
    简单对称算法
    冒泡排序(数组排序)
    二分查找
    斐波那契数列
    顺序查找(数组里查找某个元素)
    二维数组排序算法函数
  • 原文地址:https://www.cnblogs.com/da-peng/p/8290214.html
Copyright © 2011-2022 走看看