简单实现观察者模式

2022-09-08  本文已影响0人  boyiis

主题接口

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);
    }
}
上一篇下一篇

猜你喜欢

热点阅读