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

让我们通过一个天气监测应用的例子来展示观察者模式。在这个应用中,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 接口,并在接收到更新通知时显示当前的天气条件。客户端代码创建了一个天气站对象和一个显示器对象,并模拟了天气数据的变化。

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

总结

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

相关推荐
rongqing20199 小时前
Google 智能体设计模式:人机协同(HITL)
设计模式
王嘉俊92510 小时前
设计模式--享元模式:优化内存使用的轻量级设计
java·设计模式·享元模式
bkspiderx12 小时前
C++设计模式之行为型模式:中介者模式(Mediator)
c++·设计模式·中介者模式
Meteors.14 小时前
23种设计模式——责任链模式(Chain of Responsibility Pattern)
设计模式·责任链模式
o0向阳而生0o15 小时前
107、23种设计模式之观察者模式(16/23)
观察者模式·设计模式
默默coding的程序猿16 小时前
1.单例模式有哪几种常见的实现方式?
java·开发语言·spring boot·spring·单例模式·设计模式·idea
bkspiderx17 小时前
C++设计模式之行为型模式:迭代器模式(Iterator)
c++·设计模式·迭代器模式
简小瑞19 小时前
VSCode源码解密:一行代码解决内存泄漏难题
前端·设计模式·visual studio code
Asort19 小时前
JavaScript设计模式(九)——装饰器模式 (Decorator)
前端·javascript·设计模式
rongqing201919 小时前
Google 智能体设计模式:模型上下文协议 (MCP)
设计模式