美文网首页
简单实现观察者模式

简单实现观察者模式

作者: boyiis | 来源:发表于2022-09-08 11:24 被阅读0次

    主题接口

    public interface Subject {
        void registerObserver(Observer o);
        void removeObserver(Observer o);
        void notifyObserver();
    }
    
    

    观察者接口

    public interface Observer {
        void update(Message message);
    }
    

    主题内容类

    public class Message {
        public float temperature;
        public float humidity;
        public float pressure;
    
        public Message(float temperature, float humidity, float pressure){
            this.temperature = temperature;
            this.humidity = humidity;
            this.pressure = pressure;
    
        }
    }
    

    主题实现类

    public class WeatherData implements Subject {
    
        private List<Observer> observers;
    
        private Message message;
    
    
        public WeatherData() {
            observers = new ArrayList<>();
        }
    
        @Override
        public void registerObserver(Observer o) {
            observers.add(o);
    
        }
    
        @Override
        public void removeObserver(Observer o) {
            observers.remove(o);
    
        }
    
        @Override
        public void notifyObserver() {
            for (Observer o : observers) {
                o.update(message);
            }
        }
    
        public void setMeasurments(Message m) {
            this.message = m;
            notifyObserver();
        }
    }
    

    观察者实现类

    public class CurrentConditionDisplay implements Observer{
    
        private Subject weatherData;
    
        public CurrentConditionDisplay(Subject weatherData) {
            this.weatherData = weatherData;
            weatherData.registerObserver(this);
        }
    
        @Override
        public void update(Message m) {
            System.out.println("当前情况布告板(CurrentConditionDisplay):temperature:"+m.temperature+",humidity:"+m.humidity+",pressure:" + m.pressure);
    
        }
    }
    
    public class ForcastDisplay implements Observer{
        private Subject weatherData;
    
        public ForcastDisplay(Subject weatherData) {
            this.weatherData = weatherData;
            weatherData.registerObserver(this);
        }
    
        @Override
        public void update(Message m) {
            System.out.println("当前情况布告板(ForcastDisplay):temperature:"+m.temperature+",humidity:"+m.humidity+",pressure:" + m.pressure);
    
        }
    }
    
    public class StaisticsDisplay implements Observer{
        private Subject weatherData;
    
        public StaisticsDisplay(Subject weatherData) {
            this.weatherData = weatherData;
            weatherData.registerObserver(this);
        }
    
        @Override
        public void update(Message m) {
            System.out.println("当前情况布告板(StaisticsDisplay):temperature:"+m.temperature+",humidity:"+m.humidity+",pressure:" + m.pressure);
    
        }
    }
    

    测试执行

    public class Command {
    
        public static void main(String[] args) {
    
            WeatherData weatherData = new WeatherData();
    
            CurrentConditionDisplay currentConditionDisplay = new CurrentConditionDisplay(weatherData);
            ForcastDisplay forcastDisplay = new ForcastDisplay(weatherData);
            StaisticsDisplay staisticsDisplay = new StaisticsDisplay(weatherData);
    
            Message m= new Message(30, 65, 30.5F);
            weatherData.setMeasurments(m);
    
            System.out.println("_______________________________________________________________");
            m= new Message(31, 66, 30);
            weatherData.removeObserver(currentConditionDisplay);
            weatherData.setMeasurments(m);
        }
    }
    

    相关文章

      网友评论

          本文标题:简单实现观察者模式

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