观察者模式
Posted 自行车在路上
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了观察者模式相关的知识,希望对你有一定的参考价值。
在对象之间定义一对多的依赖,当一个对象改变状态,依赖它的对象都会接受到通知,并自动更新性能
文章目录
观察者模式类图
场景解读
场景:天气预报的天气温度,湿度,气压会不断的变化,但是每个的天气预报的显示这三个参数的形式会是不同的,如有的预报会用很详细的说明信息显示,有的只是显示普通数值
解析以下类图,
StatisticsDisplay和CurrentConditionsDisplay显示方式不同
StatisticsDisplay显示的是跟踪最小、平均、最大的观测值,并显示他们
CurrentConditionsDisplay显示当前观测值
当新建StatisticsDisplay,CurrentConditionsDisplay时放将他们weatherData的对象集合里面,当天气数据更改时则更新StatisticsDisplay,CurrentConditionsDisplay里显示方式的值。
代码展示
天气数据的接口和模型
管理类-注册对象,移除对象,通知对象接口
Subject
package observerMode.weatherModel;
/**
* 管理类
* @Date:Created in 15:36 2019/6/1
*/
public interface Subject
public void registerObServer(Observer o);
public void removeObserver(Observer o);
public void notifyObserver();
WeatherData-实现
package observerMode.weatherModel;
import observerMode.displayModel.Observer;
import java.util.ArrayList;
/**
* @Author:zhanCai
* @Description:
* @Date:Created in 15:47 2019/6/1
* @Modified by
* 天气数据
*/
public class WeatherData implements Subject
private ArrayList observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherData()
this.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 notifyObserver()
for (int i = 0; i < observers.size(); i++)
Observer observer = (Observer) observers.get(i);
observer.update(temperature,humidity,pressure);
public void messurementsChanged()
notifyObserver();
//温度变化
public void setMessurements(float temperature,float humidity,float pressure)
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
messurementsChanged();
观察者
观察者对象接口
Observer-更新数据
package observerMode.displayModel;
public interface Observer
public void update(float temp,float humidity,float preesure);
DisplayElement-显示接口
package observerMode.displayModel;
public interface DisplayElement
public void display();
CurrentConditionsDisplay观察者实现
package observerMode.displayModel;
import observerMode.weatherModel.Subject;
/**
* CurrentConditionsDisplay显示当前观测值
*/
public class CurrentConditionsDisplay implements Observer,DisplayElement
private float temperature;
private float humidity;
private float preesure;
private Subject weatherData;
public CurrentConditionsDisplay(Subject weatherData)
this.weatherData = weatherData;
weatherData.registerObServer(this);
public void display()
System.out.println("Current conditions:"+temperature+"F degrees and "+humidity+" % humidity");
public void update(float temperature, float humidity, float pressure)
this.temperature = temperature;
this.humidity = humidity;
this.preesure = pressure;
display();
StatisticsDisplay观察者实现
package observerMode.displayModel;
import observerMode.weatherModel.Subject;
/**
* StatisticsDisplay显示的是跟踪最小、平均、最大的观测值,并显示他们
*/
public class StatisticsDisplay implements Observer,DisplayElement
private float temperature;
private float humidity;
private float preesure;
private Subject weatherData;
public StatisticsDisplay(Subject weatherData)
this.weatherData = weatherData;
weatherData.registerObServer(this);
@Override
public void display()
System.out.println("temperature is "+temperature+" and humidity is "+humidity);
@Override
public void update(float temperature, float humidity, float pressure)
this.temperature = temperature;
this.humidity = humidity;
this.preesure = pressure;
display();
测试类
package observerMode.action;
import observerMode.displayModel.CurrentConditionsDisplay;
import observerMode.displayModel.StatisticsDisplay;
import observerMode.weatherModel.WeatherData;
import org.junit.Test;
/**
* @Date:Created in 16:07 2019/6/1
*/
public class WeatherStation
@Test
public void testWeatherObserver()
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
weatherData.setMessurements(33,44,55);
weatherData.setMessurements(66,77,88);
结果输出
Current conditions:33.0F degrees and 44.0 % humidity
temperature is 33.0 and humidity is 44.0
Current conditions:66.0F degrees and 77.0 % humidity
temperature is 66.0 and humidity is 77.0
总结:
总的来说,观察者模式看类图完全没有实现的嵌入,实现的代码都已接口去调用,这感觉很棒,低耦合。
代码github连接
https://github.com/sky5cai/DesignModeTest
以上是关于观察者模式的主要内容,如果未能解决你的问题,请参考以下文章