设计模式:观察者模式示例

让我们通过一个天气监测应用的例子来展示观察者模式。在这个应用中,WeatherStation 作为可观察的主题,它会跟踪天气数据的变化。Display 作为观察者,当天气数据更新时会显示最新的信息。

示例代码:

java 复制代码
import java.util.ArrayList;
import java.util.List;

// 观察者接口
interface Observer {
    void update(float temperature, float humidity, float pressure);
}

// 可观察对象接口
interface Observable {
    void registerObserver(Observer o);
    void removeObserver(Observer o);
    void notifyObservers();
}

// 具体可观察对象:WeatherStation
class WeatherStation implements Observable {
    private List<Observer> observers;
    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherStation() {
        observers = new ArrayList<>();
    }

    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }

    public void measurementsChanged() {
        notifyObservers();
    }

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        observers.remove(o);
    }

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

// 具体观察者:Display
class Display implements Observer {
    private Observable weatherStation;

    public Display(Observable weatherStation) {
        this.weatherStation = weatherStation;
        weatherStation.registerObserver(this);
    }

    @Override
    public void update(float temperature, float humidity, float pressure) {
        System.out.println("Current conditions: " + temperature + "C degrees and " + humidity + "% humidity");
    }

    public void unsubscribe() {
        weatherStation.removeObserver(this);
    }
}

// 客户端代码
public class ObserverPatternExample {
    public static void main(String[] args) {
        WeatherStation weatherStation = new WeatherStation();
        Display currentDisplay = new Display(weatherStation);

        // 模拟新的天气测量数据
        weatherStation.setMeasurements(28.2f, 65f, 1013.1f);
        weatherStation.setMeasurements(22.1f, 70f, 1012.5f);

        // 当不再需要显示当前天气情况时,取消订阅
        currentDisplay.unsubscribe();

        // 即使更新了天气数据,Display 也不会收到通知
        weatherStation.setMeasurements(25.5f, 60f, 1014.9f);
    }
}

在这个例子中,WeatherStation 类实现了 Observable 接口,负责管理观察者并在数据更新时通知它们。Display 类实现了 Observer 接口,并在接收到更新通知时显示当前的天气条件。客户端代码创建了一个天气站对象和一个显示器对象,并模拟了天气数据的变化。

这个例子展示了观察者模式的核心概念:对象可以订阅和取消订阅事件,而主题对象在状态变化时会通知所有的订阅者。

总结

这个天气监测应用的例子展示了观察者模式在实际应用中的使用。该模式允许对象在无需知道其他对象具体实现的情况下相互通信,从而实现了主题和观察者之间的解耦。通过使用观察者模式,我们可以轻松地添加新的观察者,或者在不影响其他观察者的情况下更改主题的状态更新逻辑。

相关推荐
Small black human8 小时前
设计模式-应用分层
设计模式
码农秋16 小时前
设计模式系列(10):结构型模式 - 桥接模式(Bridge)
设计模式·桥接模式
GodKeyNet16 小时前
设计模式-桥接模式
java·设计模式·桥接模式
N_NAN_N1 天前
类图+案例+代码详解:软件设计模式----原型模式
java·设计模式·原型模式
缘来是庄1 天前
设计模式之组合模式
java·设计模式·组合模式
DKPT1 天前
Java组合模式实现方式与测试方法
java·笔记·学习·设计模式·组合模式
鼠鼠我呀21 天前
【设计模式09】组合模式
设计模式·组合模式
N_NAN_N1 天前
类图+案例+代码详解:软件设计模式----单例模式
java·单例模式·设计模式
尤物程序猿1 天前
设计模式之代理模式--数据库查询代理和调用日志记录
设计模式·代理模式
GodKeyNet1 天前
设计模式-模板模式
设计模式·模板模式