观察者模式

1.定义
观察者模式在对象之间定义一对多的依赖,这样一来,当一个对象改变状态,依赖他的对象都会收到通知,并自动更新。
2. 案例分析
假设现有以气象系统,我们所需要完成的是当气象站数据更新时,同步更新我们的布告板(即显示装置)。
这里写图片描述
我们的需要利用WeatherData对象取得数据,并更新三个布告板:目前状况,气象统计,天气预报。
WeatherData
下面我们用类图定义观察者模式
这里写图片描述
3. 实现气象站
Subject接口类

package sxd.learn.dp;

public interface Subject{
    public void registerObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObservers();
}

Observer接口类

package sxd.learn.dp;

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

DisplayElement接口类

package sxd.learn.dp;

public interface DisplayElement{
    public void display();
}

用观察者模式实现WeatherData类

package sxd.learn.dp;

import java.util.ArrayList;

public class WeatherData implements Subject{
    private ArrayList<Observer> observers;
    private float temperture;
    private float humidity;
    private float pressure;

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

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

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

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

    public void measurementsChanged(){
        notifyObservers();
    }

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

}

布告板类CurrentConditionsDisplay

package sxd.learn.dp;

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 display() {
        // TODO Auto-generated method stub
        System.out.println("Current conditions:" + temperature
                + "F degrees and " + humidity + "% humidity");
    }

    @Override
    public void update(float temp, float humidity, float pressure) {
        // TODO Auto-generated method stub
        this.temperature = temp;
        this.humidity = humidity;
        display();
    }

}

测试程序类

package sxd.learn.dp;

public class WeatherStation {
    public static void main(String[] args){
        WeatherData weatherData = new WeatherData();
        CurrentConditionsDisplay currentDisplay = 
                new CurrentConditionsDisplay(weatherData);
//      StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
//      ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);

        weatherData.setMeasurements(80, 65, 30.4f);
        weatherData.setMeasurements(82, 70, 30.3f);
        weatherData.setMeasurements(78, 90, 29.2f);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值