设计模式-观察者模式

参考Head First 设计模式

观察者模式:定义对象之间一对多的依赖,当“一”发生改变时,所有的依赖者都会收到通知并自动改变,主题与订阅者关系。

定义主题接口Subject

/**
 * @description:
 * @author: zhangyh
 * @create :2018/6/2
 */
public interface Subject {
    public void registerObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObservers();
}

定义观察者接口Observer

/**
 * @description:
 * @author: zhangyh
 * @create :2018/6/2
 */

public interface Observer {
    public void update(float temp,float humidity,float pressure);
}

定义展示面板接口

/**
 * @description:
 * @author: zhangyh
 * @create :2018/6/2
 */
public interface DisplayElement {
    public void display();
}

定义主题实现subject接口

/**
 * @description:
 * @author: zhangyh
 * @create :2018/6/2
 */
public class WeatherData implements Subject {

    private ArrayList<Observer> observers;
    private float temperature;
    private float humidity;
    private float pressure;

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





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

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

    @Override
    public void notifyObservers() {
        for (int i =0 ;i < observers.size() ;i++){
            Observer observer = observers.get(i);
            observer.update(temperature,humidity,pressure);
        }
    }


    public void messurementChanged(){
        notifyObservers();
    }


    public void setMessurements(float temperature,float humidity,float pressure){
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        messurementChanged();
    }
}

定义观察者实现,观察者接口,展示接口

/**
 * @description:
 * @author: zhangyh
 * @create :2018/6/2
 */
public class CurrentConditionsDisplay implements Observer,DisplayElement {

    private float temperature;
    private float humidity;
    private Subject weatherData;

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


    @Override
    public void display() {

        System.out.println( "CurrentConditionsDisplay{" +
                "temperature=" + temperature +
                ", humidity=" + humidity +
                '}');
    }
}

观察者2

/**
 * @description:
 * @author: zhangyh
 * @create :2018/6/2
 */
public class ForecastConditionsDisplay implements Observer,DisplayElement {

    private float temperature;
    private float humidity;
    private Subject weatherData;

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


    @Override
    public void display() {

        System.out.println( "ForecastConditionsDisplay{" +
                "temperature=" + temperature +
                ", humidity=" + humidity +
                '}');
    }
}

测试

/**
 * @description:
 * @author: zhangyh
 * @create :2018/6/2
 */
public class WeatherDataStation {
    public static void main(String[] args){
        WeatherData weatherData = new WeatherData();
        CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);
        ForecastConditionsDisplay forecastConditionsDisplay = new ForecastConditionsDisplay(weatherData);
        weatherData.setMessurements(80,66,30.4F);
        weatherData.setMessurements(80,67,30.5F);
        weatherData.setMessurements(80,68,30.6F);
    }
}
当主题发生变化时通知各个订阅者。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值