通俗易懂的java设计模式之 --观察者模式

观察者模式(Observer Pattern)是一种设计模式,它定义了对象之间一种一对多的依赖关系,使得当一个对象状态发生改变时,它的所有依赖对象都会得到通知并自动更新。在Java中,观察者模式可以使用Java内置的观察者模式接口和类来实现。

下面是一个简单的观察者模式的例子,假设我们有一个简单的气象站应用程序,它需要定期地更新天气数据,并通知多个展示器对象显示最新的天气数据。我们使用观察者模式来实现这个功能。

  1. 首先我们需要定义一个主题(Subject)接口,它包含了注册、移除和通知观察者的方法:

public interface Subject {
    public void registerObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObservers();
}
  1. 接下来我们定义一个观察者(Observer)接口,它包含了更新数据的方法:

public interface Observer {
    public void update(float temperature, float humidity, float pressure);
}
  1. 然后我们定义一个主题类(Subject),它实现了主题接口,并包含了一个观察者列表和一些方法用于添加、删除和通知观察者:

import java.util.ArrayList;

public class WeatherData implements Subject {
    private ArrayList observers;
    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherData() {
        observers = new ArrayList();
    }
    
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    public void removeObserver(Observer o) {
        int i = observers.indexOf(o);
        if (i >= 0) {
            observers.remove(i);
        }
    }

    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(temperature, humidity, pressure);
        }
    }

    public void measurementsChanged() {
        notifyObservers();
    }

    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }
    // other WeatherData methods here
}
  1. 然后我们定义一些展示器类(Observer),它们实现了观察者接口,并包含了一个指向主题对象的引用。这里我们定义了三个展示器类:CurrentConditionsDisplay、StatisticsDisplay和ForecastDisplay。每个展示器对象都有一个 update() 方法用于接收并显示最新的天气数据。

public class CurrentConditionsDisplay implements Observer {
    private float temperature;
    private float humidity;
    private Subject weatherData;

    public CurrentConditionsDisplay(Subject weatherData) {
        this.weatherData = weatherData;
        weatherData.registerObserver(this);
    }

    public void update(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        display();
    }

    public void display() {
        System.out.println("Current conditions: " + temperature
            + "F degrees and ");
    }
}
public class StatisticsDisplay implements Observer {
       private float temperatureSum;
       private float temperatureMax;
       private float temperatureMin;
       private int numReadings;
       private Subject weatherData;
       public StatisticsDisplay(Subject weatherData) {
    this.weatherData = weatherData;
    weatherData.registerObserver(this);
    temperatureMax = Float.MIN_VALUE;
    temperatureMin = Float.MAX_VALUE;
     }

      public void update(float temperature, float humidity, float pressure) {
        temperatureSum += temperature;
        numReadings++;

      if (temperature > temperatureMax) {
              temperatureMax = temperature;
    }

      if (temperature < temperatureMin) {
              temperatureMin = temperature;
    }

      display();
   }

      public void display() {
        System.out.println("Avg/Max/Min temperature = " + (temperatureSum / numReadings)+ "/" + temperatureMax + "/" + temperatureMin);
   }
}

5.写一个测试客户端


public class WeatherStation {
    public static void main(String[] args) {
        WeatherData weatherData = new WeatherData();

        CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);
        StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
        ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);

        weatherData.setMeasurements(80, 65, 30.4f);
        weatherData.setMeasurements(82, 70, 29.2f);
        weatherData.setMeasurements(78, 90, 29.2f);
    }
}

运行程序后,我们可以看到以下输出:

Current conditions: 80.0F degrees and 65.0% humidity
Avg/Max/Min temperature = 80.0/80.0/80.0
Forecast: Improving weather on the way!
Current conditions: 82.0F degrees and 70.0% humidity
Avg/Max/Min temperature = 81.0/82.0/80.0
Forecast: Watch out for cooler, rainy weather
Current conditions: 78.0F degrees and 90.0% humidity
Avg/Max/Min temperature = 80.0/82.0/78.0
Forecast: Watch out for cooler, rainy weather

可以看到,我们的观察者模式实现成功地实现了气象站应用程序,并正确地更新了所有展示器对象的最新天气数据。

你可能感兴趣的:(设计模式,java,设计模式)