java设计模式之观察者模式

观察者模式定义了一系列的对象之间的一对多的关系。
当一个对象改变状态的时候,其他依赖者都会受到通知。

观察者模式可以比作是报纸出版商(WeatherData )和订阅者(interface Observer,CurrentConditionsDisplay ,StatisticsDisplay,ForecastDisplay )之间的关系。
如果订阅者需要收到新的报纸(temperature,humidity,pressure),则需要向报社订阅报纸(public void registerObserver(Observer o)),此时报社会把订阅者的名字写在一个名单上( private List observers),这样每次都可以从报童(interface DisplayElement)根据报社提供的名单来派发报纸(public void notifyObserver()),订阅者可以每次从报童那里得到报纸,如果有一天订阅者不再需要报纸则报社会把订阅者的名字从订阅者的名单列表中删除(public void removeObserver(Observer o))。

package tablejava;

import java.util.ArrayList;
import java.util.List;

interface Subject{

    public void registerObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObserver();
}

interface Observer{

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

interface DisplayElement{

    public void display();
}

class WeatherData implements Subject{
    private List observers;
    private float temperature;
    private float humidity;
    private float pressure;

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

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

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

    @Override
    public void notifyObserver() {
        for(int i=0;i<this.observers.size();i++) {

            Observer observer=(Observer)observers.get(i);
            observer.update(this.temperature, this.humidity, this.pressure);
        }
    }
    public void measurementsChanged() {
        notifyObserver();
    }
    public void setMeasurements(float temperature,float humidity,float pressure) {
        this.temperature=temperature;
        this.humidity=humidity;
        this.pressure=pressure;
        measurementsChanged();
    }

}


class CurrentConditionsDisplay implements Observer,DisplayElement{
    private float temperature;
    private float humidity;
    private Subject weatherData;

    public CurrentConditionsDisplay(Subject weatherData) {
        super();
        this.weatherData = weatherData;
        weatherData.registerObserver(this);
    }

    @Override
    public void display() {

        System.out.println("Current conditions:"+temperature+" F degrees and "+humidity+"% humidity");

    }

    @Override
    public void update(float temp, float humidity, float pressure) {
        this.temperature=temp;
        this.humidity=humidity;
        this.display();
    }
}
class StatisticsDisplay implements Observer,DisplayElement{
    private float temperature;
    private float humidity;
    private Subject weatherData;

    public StatisticsDisplay(Subject weatherData) {
        super();
        this.weatherData = weatherData;
        weatherData.registerObserver(this);
    }

    @Override
    public void display() {

        System.out.println("Statistics conditions:"+temperature+" F degrees and "+humidity+"% humidity");
    }

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

class ForecastDisplay implements Observer,DisplayElement{
    private float temperature;
    private float humidity;
    private Subject weatherData;

    public ForecastDisplay(Subject weatherData) {
        super();
        this.weatherData = weatherData;
        weatherData.registerObserver(this);
    }

    @Override
    public void display() {

        System.out.println("Statistics conditions:"+temperature+" F degrees and "+humidity+"% humidity");
    }

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

public class ObserverMethod {

    public static void main(String[] args) {
        WeatherData weatherData=new WeatherData();
        CurrentConditionsDisplay currentDisplay=new CurrentConditionsDisplay(weatherData);
        StatisticsDisplay statisticsDisplay=new StatisticsDisplay(weatherData);
        ForecastDisplay forecastDispaly=new ForecastDisplay(weatherData);
        weatherData.setMeasurements(80, 65, 30.4f);
        weatherData.setMeasurements(81, 66, 31.4f);
        weatherData.setMeasurements(82, 67, 32.4f);

    }

}

运行结果:

Current conditions:80.0 F degrees and 65.0% humidity
Statistics conditions:80.0 F degrees and 65.0% humidity
Statistics conditions:80.0 F degrees and 65.0% humidity
Current conditions:81.0 F degrees and 66.0% humidity
Statistics conditions:81.0 F degrees and 66.0% humidity
Statistics conditions:81.0 F degrees and 66.0% humidity
Current conditions:82.0 F degrees and 67.0% humidity
Statistics conditions:82.0 F degrees and 67.0% humidity
Statistics conditions:82.0 F degrees and 67.0% humidity

这是上面的代码的逻辑:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值