java jdk 观察者 源码,设计模式(十八)——观察者模式(JDK Observable源码分析)...

本文探讨了一个天气预报项目的实现,从最初的设计到采用观察者模式进行优化。原有的设计存在第三方接入不便和不符合开闭原则的问题。通过引入观察者模式,实现了动态添加和管理多个第三方获取气象数据,同时保持核心类WeatherData的稳定,符合ocp原则。文中还分析了JDK中的Observable类如何应用观察者模式,并展示了相关代码实现。
摘要由CSDN通过智能技术生成

1 天气预报项目需求,具体要求如下:

1) 气象站可以将每天测量到的温度,湿度,气压等等以公告的形式发布出去(比如发布到自己的网站或第三方)。

2) 需要设计开放型 API ,便于其他第三方也能接入气象站获取数据。

3) 提供温度、气压和湿度的接口

4) 测量数据更新时,要能实时的通知给第三方

2 天气预报设计方案 1-普通方案

WeatherData 类

传统的设计方案

a86b99485a7913a805796beab8e2ee01-1.png

a86b99485a7913a805796beab8e2ee01-2.png

代码实现

package com.lin.observer;

/**

* 显示当前天气情况(可以理解为气象站的网站)

* @Description:

* @author LinZM

* @date 2021-2-7 12:49:27

* @version V1.8

*/

public class CurrentConditions {

// 温度,气压,湿度

private float temperatrue;

private float pressure;

private float humidity;

// 更新天气情况

public void update(float temperature, float pressure, float humidity) {

this.temperatrue = temperature;

this.pressure = pressure;

this.humidity = humidity;

display();

}

public void display(){

System.out.println("===Today's temperature: "+temperatrue+"===");

System.out.println("===Today's pressure: "+pressure+"===");

System.out.println("===Today's humidity: "+humidity+"===");

}

}

package com.lin.observer;

/*

* 类是核心 1. 包含最新的天气情况信息 2. 含有 CurrentConditions 对象 3. 当数据有更新时,就主动的调用

* CurrentConditions 对象 update 方法(含 display), 这样他们(接入方)就看到最新的信息

*/

public class WeatherData {

private float temperatrue;

private float pressure;

private float humidity;

private CurrentConditions currentConditions;

//加入新的第三方

public WeatherData(CurrentConditions currentConditions) {

this.currentConditions = currentConditions;

}

public float getTemperature() {

return temperatrue;

}

public float getPressure() {

return pressure;

}

public float getHumidity() {

return humidity;

}

public void dataChange() {

//调用 接入方的 update

currentConditions.update(getTemperature(), getPressure(), getHumidity());

}

//当数据有更新时,就调用 setData

public void setData(float temperature, float pressure, float humidity) {

this.temperatrue = temperature;

this.pressure = pressure;

this.humidity = humidity;

//调用 dataChange, 将最新的信息 推送给 接入方 currentConditions

dataChange();

}

}

package com.lin.observer;

public class Client {

public static void main(String[] args) {

CurrentConditions currentConditions = new CurrentConditions();

WeatherData weatherData = new WeatherData(currentConditions);

weatherData.setData(10, 20, 3);

}

}

a86b99485a7913a805796beab8e2ee01-3.png

问题分析

1) 其他第三方接入气象站获取数据的问题

2) 无法在运行时动态的添加第三方 (新浪网站)

3) 违反 ocp 原则=>观察者模式

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

public void dataChange() {

currentConditions.update(getTemperature(), getPressure(), getHumidity());

}

3 观察者模式原理

1) 观察者模式类似订牛奶业务

2) 奶站/气象局:Subject

3) 用户/第三方网站:Observer

Subject:登记注册、移除和通知

1) registerObserver 注 册

2) removeObserver 移 除

3) notifyObservers() 通知所有的注册的用户,根据不同需求,可以是更新数据,让用户来取,也可能是实施推送, 看具体需求定

Observer:接收输入

观察者模式:对象之间多对一依赖的一种设计方案,被依赖的对象为 Subject ,依赖的对象为 Observer,Subject

通知 Observer 变化, 比如这里的奶站是 Subject ,是 1 的一方。用户时 Observer,是多的一方。

4 观察者模式解决天气预报需求

类图说明

a86b99485a7913a805796beab8e2ee01-4.png

代码实现

package com.lin.observer.plus;

public interface Subject {

void registerObserver(Observer o);

void removeObserver(Observer o);

void notifyObserver();

}

package com.lin.observer.plus;

import java.util.ArrayList;

/*

* 类是核心 1. 包含最新的天气情况信息 2. 含有 CurrentConditions 对象 3. 当数据有更新时,就主动的调用

* CurrentConditions 对象 update 方法(含 display), 这样他们(接入方)就看到最新的信息

*/

public class WeatherData implements Subject{

private float temperatrue;

private float pressure;

private float humidity;

private ArrayList observers;

//加入新的第三方

public WeatherData() {

observers = new ArrayList();

}

public float getTemperature() {

return temperatrue;

}

public float getPressure() {

return pressure;

}

public float getHumidity() {

return humidity;

}

public void dataChange() {

//调用 接入方的 update

//currentConditions.update(getTemperature(), getPressure(), getHumidity());

notifyObserver();

}

//当数据有更新时,就调用 setData

public void setData(float temperature, float pressure, float humidity) {

this.temperatrue = temperature;

this.pressure = pressure;

this.humidity = humidity;

//调用 dataChange, 将最新的信息 推送给 接入方 currentConditions

dataChange();

}

@Override

public void registerObserver(Observer o) {

observers.add(o);

}

@Override

public void removeObserver(Observer o) {

if(observers.contains(o)) {

observers.remove(o);

}

}

@Override

public void notifyObserver() {

for (int i = 0; i < observers.size(); i++) {

observers.get(i).update(temperatrue, pressure, humidity);

}

}

}

package com.lin.observer.plus;

public interface Observer {

void update(float temperatrue, float pressure, float humidity);

}

package com.lin.observer.plus;

public class CurrentConditions implements Observer{

// 温度,气压,湿度

private float temperatrue;

private float pressure;

private float humidity;

// 更新天气情况

public void update(float temperature, float pressure, float humidity) {

this.temperatrue = temperature;

this.pressure = pressure;

this.humidity = humidity;

display();

}

public void display(){

System.out.println("===Today's temperature: "+temperatrue+"===");

System.out.println("===Today's pressure: "+pressure+"===");

System.out.println("===Today's humidity: "+humidity+"===");

}

}

package com.lin.observer.plus;

public class BaiDu implements Observer{

// 温度,气压,湿度

private float temperatrue;

private float pressure;

private float humidity;

// 更新天气情况

public void update(float temperature, float pressure, float humidity) {

this.temperatrue = temperature;

this.pressure = pressure;

this.humidity = humidity;

display();

}

public void display() {

System.out.println("===BaiDu's temperature: " + temperatrue + "===");

System.out.println("===BaiDu's pressure: " + pressure + "===");

System.out.println("===BaiDu's humidity: " + humidity + "===");

}

}

package com.lin.observer.plus;

public class Client {

public static void main(String[] args) {

// 创建一个WeatherData

WeatherData weatherData = new WeatherData();

// 创建观察者

CurrentConditions currentConditions = new CurrentConditions();

BaiDu baiDu = new BaiDu();

// 注册

weatherData.registerObserver(currentConditions);

weatherData.registerObserver(baiDu);

// 测试

System.out.println("通知各个注册的观察者:");

weatherData.setData(23, 12, -0.4f);

}

}

a86b99485a7913a805796beab8e2ee01-5.png

观察者模式的好处

1) 观察者模式设计后,会以集合的方式来管理用户(Observer),包括注册,移除和通知。

2) 这样,我们增加观察者(这里可以理解成一个新的公告板) ,就不需要去修改核心类 WeatherData  不会修改代码, 遵守了 ocp 原则。

5 观察者模式在 Jdk 应用的源码分析

1) Jdk 的 Observable 类就使用了观察者模式

2) 代码分析+模式角色分析

a86b99485a7913a805796beab8e2ee01-6.png

3) 模式角色分析

Observable 的作用和地位等价于 我们前面讲过 Subject

Observable  是类,不是接口,类中已经实现了核心的方法 , 即管理 Observer  的方法 add.. delete .. notify...

Observer  的作用和地位等价于我们前面讲过的 Observer, 有 update

Observable  和 Observer  的使用方法和前面讲过的一样,只是 Observable 是类,通过继承来实现观察者模式

仅供参考,有错误还请指出!

有什么想法,评论区留言,互相指教指教。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值