当前位置: 首页 > news >正文

设计模式之观察者模式

什么是观察者模式

    观察者模式定义了对象之间一种一对多依赖关系,使得每当一个对象状态发生改变时,其相关依赖对象都能收到通知并自动刷新。
    观察者模式主要包含以下几个角色:
        Subject(目标):指被观察的对象,在目标中定义了一个观察者集合,一个观察目标可以接受任意数量的观察者来观察。提供了一系列方法来增加,删除观察者对象,同时也定义了通知方法。目标类可以是接口也可以是抽象类。
        ConcreteSubject(具体目标):目标类的子类,通常包含经常发生改变的数据,当他的状态发生改变时,向他的各个观察者发出通知。
        Observer(观察者):对观察目标的改变做出反应,观察者一般定义为接口,该接口声明了更新数据的方法。
        ConcreteObserver(具体观察者):维护了一个指定具体目标对象的引用,他存储具体观察者的有关状态,这些状态和具体目标的状态要保持一致。

观察者模式的优缺点

优点

  1. 可以实现表示层和数据逻辑层的分离,定义了稳定的消息更新传递机制,并抽象了更新接口,使得可以有各种各样的表示层充当具体观察者。
  2. 在观察目标和观察者之间建立了一个抽象的耦合。观察目标只需要维持一个抽象观察者的集合,无需了解具体观察者。
  3. 支持广播通信,观察目标会向所有已注册的观察者对象发送通知。
  4. 符合开闭原则,增加新的观察者对象无需修改代码。

缺点

  1. 如果一个观察目标对象有很多直接或间接的观察者,通知到所有的观察者会很耗时间。
  2. 如果在观察者和观察对象之间存在循环依赖就会触发循环调用。

观察者模式的应用场景

  1. 一个抽象模型有两个方面,一方面依赖于另一方面,将这两方面封装在独立的对象中使他们可以各自独立的改变。
  2. 一个对象的改变将导致一个或多个对象也发生改变。

观察者模式的案例

// 抽象目标类
public interface Subject {void registerObserver(Observer o);void removeObserver(Observer o);void notifyObservers();}// 具体目标类
public class WeatherData implements Subject {private float temperatrue;private float pressure;private float humidity;private ArrayList<Observer> observers;public WeatherData() {observers = new ArrayList<Observer>();}public float getTemperature() {return temperatrue;}public float getPressure() {return pressure;}public float getHumidity() {return humidity;}public void dataChange() {//调用 接入方的 updatenotifyObservers();}public void setData(float temperature, float pressure, float humidity) {this.temperatrue = temperature;this.pressure = pressure;this.humidity = humidity;//调用dataChange, 将最新的信息 推送给 接入方 currentConditionsdataChange();}@Overridepublic void registerObserver(Observer o) {observers.add(o);}@Overridepublic void removeObserver(Observer o) {if (observers.contains(o)) {observers.remove(o);}}@Overridepublic void notifyObservers() {for (Observer observer : observers) {observer.update(this.temperatrue, this.pressure, this.humidity);}}}// 抽象观察者
public interface Observer {void update(float temperature, float pressure, float humidity);
}// 具体观察者
public class BaiduSite implements Observer {private float temperature;private float pressure;private float humidity;@Overridepublic void update(float temperature, float pressure, float humidity) {this.temperature = temperature;this.pressure = pressure;this.humidity = humidity;display();}public void display() {System.out.println("===百度网站====");System.out.println("***百度网站 气温 : " + temperature + "***");System.out.println("***百度网站 气压: " + pressure + "***");System.out.println("***百度网站 湿度: " + humidity + "***");}}public class CurrentConditions implements Observer {private float temperature;private float pressure;private float humidity;@Overridepublic void update(float temperature, float pressure, float humidity) {this.temperature = temperature;this.pressure = pressure;this.humidity = humidity;display();}public void display() {System.out.println("***Today mTemperature: " + temperature + "***");System.out.println("***Today mPressure: " + pressure + "***");System.out.println("***Today mHumidity: " + humidity + "***");}
}public static void main(String[] args) {//创建一个WeatherDataWeatherData weatherData = new WeatherData();//创建观察者CurrentConditions currentConditions = new CurrentConditions();BaiduSite baiduSite = new BaiduSite();//注册到weatherDataweatherData.registerObserver(currentConditions);weatherData.registerObserver(baiduSite);//测试System.out.println("通知各个注册的观察者, 看看信息");weatherData.setData(10f, 100f, 30.3f);weatherData.removeObserver(currentConditions);//测试System.out.println();System.out.println("通知各个注册的观察者, 看看信息");weatherData.setData(10f, 100f, 30.3f);
}

在这里插入图片描述

观察者模式在源码中的应用

Observer

// 抽象观察者
public interface Observer {/*** This method is called whenever the observed object is changed. An* application calls an <tt>Observable</tt> object's* <code>notifyObservers</code> method to have all the object's* observers notified of the change.** @param   o     the observable object.* @param   arg   an argument passed to the <code>notifyObservers</code>*                 method.*/void update(Observable o, Object arg);
}// 具体目标类
public class Observable {private boolean changed = false;// 维护了观察者集合private Vector<Observer> obs;/** Construct an Observable with zero Observers. */public Observable() {obs = new Vector<>();}public synchronized void addObserver(Observer o) {if (o == null)throw new NullPointerException();if (!obs.contains(o)) {obs.addElement(o);}}/*** Deletes an observer from the set of observers of this object.* Passing <CODE>null</CODE> to this method will have no effect.* @param   o   the observer to be deleted.*/public synchronized void deleteObserver(Observer o) {obs.removeElement(o);}public void notifyObservers(Object arg) {/** a temporary array buffer, used as a snapshot of the state of* current Observers.*/Object[] arrLocal;synchronized (this) {/* We don't want the Observer doing callbacks into* arbitrary code while holding its own Monitor.* The code where we extract each Observable from* the Vector and store the state of the Observer* needs synchronization, but notifying observers* does not (should not).  The worst result of any* potential race-condition here is that:* 1) a newly-added Observer will miss a*   notification in progress* 2) a recently unregistered Observer will be*   wrongly notified when it doesn't care*/if (!changed)return;arrLocal = obs.toArray();clearChanged();}for (int i = arrLocal.length-1; i>=0; i--)((Observer)arrLocal[i]).update(this, arg);}......
}

http://www.lryc.cn/news/11545.html

相关文章:

  • Java Lambda表达式
  • 【1237. 找出给定方程的正整数解】
  • java基础学习 day41(继承中成员变量和成员方法的访问特点,方法的重写)
  • 【c语言进阶】深度剖析整形数据
  • 【信息系统项目管理师】项目管理十大知识领域记忆敲出(采购风险沟通干系人)
  • [LeetCode 1237]找出给定方程的正整数解
  • 6.2 构建 RESTful 应用接口
  • 20230218英语学习
  • Linux单一服务管理systemctl
  • 【GStreamer 】 TX1中CPU和GPU解码显示海康相机RTSP流
  • 匿名内部类、Lambda表达式、方法引用对比分析
  • ESXi主机CVE-2021-21972漏洞复现安全处置建议
  • 研报精选230217
  • c++11 标准模板(STL)(std::unordered_set)(一)
  • 【C语言进阶】你听说过柔性数组吗?
  • [LeetCode]1237. 找出给定方程的正整数解
  • 【路径规划】基于A*算法和Dijkstra算法的路径规划(Python代码实现)
  • 蓝桥杯 stm32 PWM 设置占空比
  • React 合成事件理解
  • 202302|读书笔记——国图点滴
  • Linux 操作系统原理 — NUMA 架构中的多线程调度开销与性能优化
  • OpenGL - 如何理解 VAO 与 VBO 之间的关系
  • Linux中sed的使用
  • [软件工程导论(第六版)]第1章 软件工程学概述(复习笔记)
  • ISP相关
  • vTESTstudio - VT System CAPL Functions - VT2004(续1)
  • WeakMap弱引用
  • Springboot 使用quartz 定时任务 增删改查
  • 华为OD机试 - 猜字谜(Python) | 机试题+算法思路 【2023】
  • Linux常用命令汇总