《head First设计模式》阅读笔记02_观察者模式

1.由气象站应用系统设计引出观察者模式

1.1.系统需求分析

1.1.2.系统组成结构

1.2.系统设计

1.2.1.系统设计准备阶段

1.2.2.系统设计--解决方案

1.2.2.1.错误的方案

 1.2.2.2.基于观察者模式设计方案

 

 1.3.系统代码实现

1.3.1.定义相关接口

1.3.1.1.创建顶层主题接口
package com.wf.observer.demo.wheatherapp;

/**
 * @ClassName Subject
 * @Description 顶层主题接口
 * @Author wf
 * @Date 2020/6/30 16:36
 * @Version 1.0
 */
public interface Subject {
    //观察者订阅【注册】主题
    void registerObserver(Observer observer);
    //取消订阅
    void removeObserver(Observer observer);
    //主题数据更新时,通知所有观察者同步更新
    void notifyObservers();
}
1.3.1.2.观察者顶层接口
package com.wf.observer.demo.wheatherapp;

/**
 * @ClassName Observer
 * @Description 顶层观察者接口
 * @Author wf
 * @Date 2020/6/30 16:39
 * @Version 1.0
 */
public interface Observer {
    /**
     * @param temp 温度
     * @param humility 湿度
     * @param pressure 气压
     */
    void update(float temp, float humility,float pressure);
}

 思考:

  这里的upate方法,传参具体的数据,这些变化的数据,是否需要进行封装呢?

1.3.1.3.定义布告板数据展示接口
package com.wf.observer.demo.wheatherapp;

/**
 * @ClassName DisplayElement
 * @Description 布告板展示接口
 * @Author wf
 * @Date 2020/6/30 16:46
 * @Version 1.0
 */
public interface DisplayElement {
    void display();
}

1.3.2.实现相关接口

1.3.2.1.主题接口子类实现
package com.wf.observer.demo.wheatherapp;

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

/**
 * @ClassName WeatherData
 * @Description 主题接口实现子类
 * @Author wf
 * @Date 2020/6/30 15:05
 * @Version 1.0
 */
public class WeatherData implements Subject {
    private List<Observer> observerList;
    private float temperature;
    private float humidity;
    private float pressure;

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

    public void measurementsChanged(){
        notifyObservers();
    }

    public void registerObserver(Observer observer) {
        this.observerList.add(observer);
    }

    public void removeObserver(Observer observer) {
        int index = this.observerList.indexOf(observer);
        if(index > 0){
            this.observerList.remove(index);
        }
    }

    public void notifyObservers() {
        //通知所有
        for (Observer observer : this.observerList) {
            observer.update(temperature,humidity,pressure);
        }
    }

    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }
    //其它方法
}
 1.3.2.2.布告板实现

  布告板展示功能实现,实际者上是观察者实现子类。代码如下:

package com.wf.observer.demo.wheatherapp;

/**
 * @ClassName CurrentConditionDisplay
 * @Description 当前状况实现,作为观察者
 * @Author wf
 * @Date 2020/6/30 17:00
 * @Version 1.0
 */
public class CurrentConditionDisplay implements Observer, DisplayElement {
    private Subject weatherData;
    private float temperature;
    private float humidity;
    private float pressure;

    public CurrentConditionDisplay(Subject weatherData) {
        this.weatherData = weatherData;
        //具体的观察者注册
        weatherData.registerObserver(this);
    }

    public void display() {
        System.out.println("current condition:"+temperature + "F degree and " + humidity + "% humidity");
    }

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

1.3.3.启动气象站,进行测试 

 

package com.wf.observer.demo.wheatherapp;

/**
 * @ClassName WeatherStation
 * @Description 启动气象站,测试
 * @Author wf
 * @Date 2020/6/30 17:11
 * @Version 1.0
 */
public class WeatherStation {
    public static void main(String[] args) {
        //从气象站获取原始数据,模拟
        WeatherData data = new WeatherData();

        //创建三个布告板
        CurrentConditionDisplay currentDisplay = new CurrentConditionDisplay(data);

        //模拟气象数据
        data.setMeasurements(80,65,34.5f);
    }
}

测试结果:

原文地址:https://www.cnblogs.com/wfdespace/p/13214964.html