设计模式 - 观察者模式

普通方案

天气情况:

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

    public void display(){
        System.out.println("***Today mTemperature: "+temperature+"***");
        System.out.println("***Today mPressure: "+pressure+"***");
        System.out.println("***Today mHumidity: "+humidity+"***");
    }
}

天气数据:

public class WeatherData {
    private float temperature;
    private float pressure;
    private float humidity;

    private CurrentConditions currentConditions;
    public WeatherData(CurrentConditions currentConditions) {
        this.currentConditions = currentConditions;
    }

    public float getTemperature(){
        return temperature;
    }

    public float getPressure() {
        return pressure;
    }

    public float getHumidity() {
        return humidity;
    }

    public void dataChange() {
        currentConditions.update(getTemperature(), getPressure(),getHumidity());
    }

    public void setData(float temperature, float pressure, float humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        dataChange();
    }
}

天气展示:

public class InternetWeater {
    public static void main(String[] args) {
        CurrentConditions currentConditions = new CurrentConditions();
        WeatherData weatherData = new WeatherData(currentConditions);
        weatherData.setData(30, 150,40);
    }
}

缺点:

在WeatherData中,当增加一个第三方,都需要创建一个对应的第三方的公告板 对象,并加入到dataChange, 不利于维护,也不是动态加入

观察者模式:

  • 观察者模式设计后,会以集合方式来管理用户(Observer),包括注册,移除和通知
  • 我们增加观察者,就不需要去修改核心类 WeahterData 代理吗,遵守了 ocp 原则

主题:

/**
 * 接口,让 WeatherData 来实现
 */
public interface Subject {
    public void reegisterObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObservers();
}

天气数据:

import java.util.ArrayList;

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

    // 观察者集合
    private ArrayList observers;

    public WeatherData() {
        this.observers = new ArrayList<>();
    }

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

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

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

    // 当数据有更新时,就调用setData
    public void setData(float temperature, float pressure, float humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        dataChange();
    }

    public void dataChange(){
        notifyObservers();
    }
}

观察者:

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

观察者实现一:

public class BaiduSite implements Observer{
    private float temperature;
    private float pressure;
    private float humidity;

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

    // 显示
    public void display() {
        System.out.println("===百度网站====");
        System.out.println("***百度网站 气温 : " + temperature + "***");
        System.out.println("***百度网站 气压: " + pressure + "***");
        System.out.println("***百度网站 湿度: " + humidity + "***");
    }
}

观察者实现二:

public class CurrentConditions implements Observer{
    private float temperature;
    private float pressure;
    private float humidity;

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

    public void display(){
        System.out.println("***Today mTemperature: " + temperature + "***");
        System.out.println("***Today mPressure: " + pressure + "***");
        System.out.println("***Today mHumidity: " + humidity + "***");
    }

}

执行:

public class Client {
    public static void main(String[] args) {
        WeatherData weatherData = new WeatherData();
        CurrentConditions currentConditions = new CurrentConditions();
        BaiduSite baiduSite = new BaiduSite();
        weatherData.reegisterObserver(currentConditions);
        weatherData.reegisterObserver(baiduSite);
        weatherData.setData(10f,100f,30.3f);

        weatherData.removeObserver(currentConditions);
        weatherData.setData(20f,150f, 35.5f);
    }
}

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