基本需求:

  • 气象站可以将每天测量到的温度,湿度,气压等等,以公告的形式发布出去(比如发布到自己的网站或第三方)
  • 需要设计开放型API,便于其他第三方也能接入气象站获取数据
  • 提供温度、气压和湿度的接口
  • 测量数据更新时,要能实时的通知给第三方

传统方案:

  • 通过对需求的分析,我们可以设计一个WeatherData类,其中包含getXxx()方法,可以让第三方接入,并得到相关信息

  • 当有数据更新时,WeatherData调用dataChange()方法去更新数据,当第三方获取时,就能获取到最新的数据,当然也可以推送

  • WeatherData内部还需维护一个CurrentCondition对象,便于完成推送或通知

  • UML类图

  • 代码实现

      1. public class CurrentCondition {
      2. // 被通知的对象类
      3. // 温度
      4. private double temperature;
      5. // 压力
      6. private double pressure;
      7. // 湿度
      8. private double humidity;
      9. public void update(double temperature, double pressure, double humidity) {
      10. this.temperature = temperature;
      11. this.pressure = pressure;
      12. this.humidity = humidity;
      13. display();
      14. }
      15. private void display() {
      16. System.out.println("======CurrentCondition======");
      17. System.out.println("温度:" + this.temperature);
      18. System.out.println("压力:" + this.pressure);
      19. System.out.println("湿度:" + this.humidity);
      20. }
      21. }
      1. public class WeatherData {
      2. // 天气数据类
      3. // 温度
      4. private double temperature;
      5. // 压力
      6. private double pressure;
      7. // 湿度
      8. private double humidity;
      9. // 聚合天气数据发生改变需要通知或者被推送的类
      10. private CurrentCondition currentCondition;
      11. public WeatherData(CurrentCondition currentCondition) {
      12. this.currentCondition = currentCondition;
      13. }
      14. // 将天气数据的改变通知给需要通知或者被推送的类 被通知的类也可以通过getXxx()方法自己获取数据
      15. private void dataChange() {
      16. currentCondition.update(this.temperature, this.pressure, this.humidity);
      17. }
      18. // 更改天气数据
      19. public void setData(double temperature, double pressure, double humidity) {
      20. this.temperature = temperature;
      21. this.pressure = pressure;
      22. this.humidity = humidity;
      23. dataChange();
      24. }
      25. }
      1. public class Client {
      2. public static void main(String[] args) {
      3. WeatherData weatherData = new WeatherData(new CurrentCondition());
      4. // 更新天气数据 就会通知应用或者第三方
      5. weatherData.setData(10d, 20d, 30d);
      6. System.out.println("======天气情况发生变换======");
      7. weatherData.setData(20d, 30d, 40d);
      8. }
      9. }
    • 问题分析

      • 其他第三方接入气象站获取数据问题
      • 无法在运行时动态的添加第三方
      • 违反了ocp原则,在WeatherData中,如果需要增加新的第三方应用,都需要创建一个对应的第三方的公告板对象,并加入到dataChange()方法中,不利于维护,也不是动态加入

基本介绍:

  • 当对象间存在一对多关系时,则使用观察者模式(Observer),比如,当一个对象被修改时,则会自动通知依赖它的对象,观察者模式属于行为型模式

  • 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新

  • 一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知,进行广播通知

  • 基本原理

    • 观察者模式类似订牛奶业务 例如:奶站/气象局:Subject 和 用户/第三方网站:Observer
    • Subject:登记注册、移除和通知 一的一方(被观察者)
      • registerObserver 注册
      • removeObserver 移除
      • notifyObservers() 通知所有的注册的用户,根据不同需求,可以是更新数据,让用户来取,也可能是实施推送,看具体需求定
    • Observer:接收输入 也就是第三方,可以有多个实现 多个一方(观察者)
    • 对象之间多对一依赖的一种设计方案,被依赖的对象为Subject,依赖的对象为Observer,Subject通知Observer变化,比如这里的奶站是Subject,是1的一方。用户时Observer,是多的一方
  • UML类图(案例)

  • 代码实现

      1. public interface Subject {
      2. // 被观察者接口 作为一的一方 ,需要聚合多个观察者 可以使用List进行管理
      3. // 注册观察者
      4. void register(Observer observer);
      5. // 移除观察者
      6. void remove(Observer observer);
      7. // 被观察者状态发生改变,进行通知所有的观察者
      8. void notifyObservers();
      9. }
      10. // 实现类
      11. class WeatherData implements Subject {
      12. // 天气数据类 作为被观察者 一的一方
      13. // 温度
      14. private double temperature;
      15. // 压力
      16. private double pressure;
      17. // 湿度
      18. private double humidity;
      19. // 聚合所有的观察者进行管理 动态进行修改 使用List集合管理观察者们
      20. private List<Observer> observers;
      21. public WeatherData() {
      22. this.observers = new ArrayList<Observer>();
      23. }
      24. // 更改天气数据
      25. public void setData(double temperature, double pressure, double humidity) {
      26. this.temperature = temperature;
      27. this.pressure = pressure;
      28. this.humidity = humidity;
      29. // 被观察者状态发生改变,通知注册的所有观察者
      30. notifyObservers();
      31. }
      32. @Override
      33. public void register(Observer observer) {
      34. if (!this.observers.contains(observer)) {
      35. observers.add(observer);
      36. }
      37. }
      38. @Override
      39. public void remove(Observer observer) {
      40. this.observers.remove(observer);
      41. }
      42. @Override
      43. public void notifyObservers() {
      44. // 遍历观察者的集合 对所有的观察者进行通知
      45. observers.forEach(observer -> observer.update(this.temperature, this.pressure, this.humidity));
      46. }
      47. }
      1. public interface Observer {
      2. // 观察者接口 作为多的一方
      3. // 被观察者改变状态时,通知观察者所调用的方法
      4. void update(double temperature, double pressure, double humidity);
      5. }
      6. // 子类一
      7. class CurrentCondition implements Observer{
      8. // 被通知的对象类
      9. // 温度
      10. private double temperature;
      11. // 压力
      12. private double pressure;
      13. // 湿度
      14. private double humidity;
      15. public void update(double temperature, double pressure, double humidity) {
      16. this.temperature = temperature;
      17. this.pressure = pressure;
      18. this.humidity = humidity;
      19. display();
      20. }
      21. private void display() {
      22. System.out.println("======CurrentCondition======");
      23. System.out.println("温度:" + this.temperature);
      24. System.out.println("压力:" + this.pressure);
      25. System.out.println("湿度:" + this.humidity);
      26. }
      27. }
      28. // 子类二
      29. class BaiDu implements Observer{
      30. // 被通知的对象类
      31. // 温度
      32. private double temperature;
      33. // 压力
      34. private double pressure;
      35. // 湿度
      36. private double humidity;
      37. public void update(double temperature, double pressure, double humidity) {
      38. this.temperature = temperature;
      39. this.pressure = pressure;
      40. this.humidity = humidity;
      41. display();
      42. }
      43. private void display() {
      44. System.out.println("======BaiDu======");
      45. System.out.println("温度:" + this.temperature);
      46. System.out.println("压力:" + this.pressure);
      47. System.out.println("湿度:" + this.humidity);
      48. }
      49. }
      1. public class Client {
      2. public static void main(String[] args) {
      3. // 创建观察者
      4. CurrentCondition currentCondition = new CurrentCondition();
      5. // 创建被观察者
      6. WeatherData weatherData = new WeatherData();
      7. // 向被观察者中注册观察者
      8. weatherData.register(currentCondition);
      9. // 改变被观察者的状态 观察者就会收到通知
      10. weatherData.setData(10d, 20d, 30d);
      11. // 后续业务扩展 加入新的第三方
      12. BaiDu baiDu = new BaiDu();
      13. weatherData.register(baiDu);
      14. System.out.println("======加入了新的第三方======");
      15. weatherData.setData(20d, 30d, 40d);
      16. }
      17. }

jdk源码:

  • 在jdk的Observer类和Observable类就使用到了观察者模式

  • Observer作为了观察者接口,提供了update()方法

  • Observable相当于Subject,作为被观察者,只不过在jdk源码中,Observable是一个类,而不是接口,提供了注册观察者,删除观察者,通知观察者的一系列管理观察者的方法

    1. // 观察者接口
    2. public interface Observer {
    3. /**
    4. * This method is called whenever the observed object is changed. An
    5. * application calls an <tt>Observable</tt> object's
    6. * <code>notifyObservers</code> method to have all the object's
    7. * observers notified of the change.
    8. *
    9. * @param o the observable object.
    10. * @param arg an argument passed to the <code>notifyObservers</code>
    11. * method.
    12. */
    13. void update(Observable o, Object arg);
    14. }
    15. // 被观察者 相当于Subject
    16. public class Observable {
    17. private boolean changed = false;
    18. private Vector<Observer> obs;
    19. /** Construct an Observable with zero Observers. */
    20. public synchronized void addObserver(Observer o) {
    21. if (o == null)
    22. throw new NullPointerException();
    23. if (!obs.contains(o)) {
    24. obs.addElement(o);
    25. }
    26. }
    27. /**
    28. * Deletes an observer from the set of observers of this object.
    29. * Passing <CODE>null</CODE> to this method will have no effect.
    30. * @param o the observer to be deleted.
    31. */
    32. public synchronized void deleteObserver(Observer o) {
    33. obs.removeElement(o);
    34. }
    35. /**
    36. * If this object has changed, as indicated by the
    37. * <code>hasChanged</code> method, then notify all of its observers
    38. * and then call the <code>clearChanged</code> method to
    39. * indicate that this object has no longer changed.
    40. * <p>
    41. * Each observer has its <code>update</code> method called with two
    42. * arguments: this observable object and <code>null</code>. In other
    43. * words, this method is equivalent to:
    44. * <blockquote><tt>
    45. * notifyObservers(null)</tt></blockquote>
    46. *
    47. * @see java.util.Observable#clearChanged()
    48. * @see java.util.Observable#hasChanged()
    49. * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
    50. */
    51. public void notifyObservers() {
    52. notifyObservers(null);
    53. }
    54. /**
    55. * If this object has changed, as indicated by the
    56. * <code>hasChanged</code> method, then notify all of its observers
    57. * and then call the <code>clearChanged</code> method to indicate
    58. * that this object has no longer changed.
    59. * <p>
    60. * Each observer has its <code>update</code> method called with two
    61. * arguments: this observable object and the <code>arg</code> argument.
    62. *
    63. * @param arg any object.
    64. * @see java.util.Observable#clearChanged()
    65. * @see java.util.Observable#hasChanged()
    66. * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
    67. */
    68. public void notifyObservers(Object arg) {
    69. /*
    70. * a temporary array buffer, used as a snapshot of the state of
    71. * current Observers.
    72. */
    73. Object[] arrLocal;
    74. synchronized (this) {
    75. /* We don't want the Observer doing callbacks into
    76. * arbitrary code while holding its own Monitor.
    77. * The code where we extract each Observable from
    78. * the Vector and store the state of the Observer
    79. * needs synchronization, but notifying observers
    80. * does not (should not). The worst result of any
    81. * potential race-condition here is that:
    82. * 1) a newly-added Observer will miss a
    83. * notification in progress
    84. * 2) a recently unregistered Observer will be
    85. * wrongly notified when it doesn't care
    86. */
    87. if (!changed)
    88. return;
    89. arrLocal = obs.toArray();
    90. clearChanged();
    91. }
    92. for (int i = arrLocal.length-1; i>=0; i--)
    93. ((Observer)arrLocal[i]).update(this, arg);
    94. }
    95. }

注意事项:

  • 观察者和被观察者是抽象耦合的,建立一套触发机制
  • 如果一个被观察者对象有很多的直接和间接的观察者的话,将所有的观察者都通知到会花费很多时间
  • 如果在观察者和观察目标之间有循环依赖的话,观察目标会触发它们之间进行循环调用,可能导致系统崩溃
  • 观察者模式没有相应的机制让观察者知道所观察的目标对象是怎么发生变化的,而仅仅只是知道观察目标发生了变化
  • JAVA中已经有了对观察者模式的支持类
  • 避免循环引用
  • 如果顺序执行,某一观察者错误会导致系统卡壳,一般采用异步方式

17.java设计模式之观察者模式的更多相关文章

  1. 理解java设计模式之观察者模式

    在生活实际中,我们经常会遇到关注一个事物数据变化的情况,例如生活中的温度记录仪,当温度变化时,我们观察它温度变化的曲线,温度记录日志等.对于这一类问题,很接近java设计模式里面的“观察者模式”,它适 ...

  2. java设计模式之观察者模式以及在java中作用

    观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式.模型-视图(Model/View)模式.源-监听器(Source/Listener)模式或从属者(Dependen ...

  3. java设计模式02观察者模式

    观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己. 这里主要讲一下学习内置观察者的记录,在JA ...

  4. java设计模式之观察者模式

    观察者模式 观察者模式(有时又被称为发布(publish )-订阅(Subscribe)模式.模型-视图(View)模式.源-收听者(Listener)模式或从属者模式)是软件设计模式的一种.在此种模 ...

  5. JAVA设计模式 之 观察者模式

    简介: 观察者模式是JDK中最多的设计模式之一,非常有用,观察者模式介绍了一对多的依赖关系及松耦合,有了观察者,你将会消息灵通. 认识观察者模式,看一个报纸.杂志订阅是怎么回事: (1). 报社的业务 ...

  6. 折腾Java设计模式之观察者模式

    观察者模式 Define a one-to-many dependency between objects where a state change in one object results in ...

  7. java设计模式之-观察者模式(发布-订阅模式)

    1.观察者模式定义  观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象. 这个主题对象在状态上发生变化时,会通知所有观察者对象,让它们能够自动更新自己. 2.观察者模式结构 ...

  8. JAVA设计模式之观察者模式 - Observer

    有趣的事情发生时,可千万别错过了!有一个模式可以帮你的对象知悉现况,不会错过该对象感兴趣的事.对象甚至在运行时可决定是否要继续被通知.有了观察者,你将会消息灵通. 介绍 观察者模式的定义: 在对象之间 ...

  9. JAVA设计模式 之 观察者模式(JDK内置实现)

    简介:使用JAVA内置的帮你搞定观察者模式. 1. 先把类图放在这里: (1). Observable类追踪所有的观察者,并通知他们. (2). Observer这个接口看起来很熟悉,它和我们之前写的 ...

随机推荐

  1. adb命令如何获取appPackage和appActivity的信息

    如何获取appPackage和appActivity的信息,这里有一个极为实用的命令:adb shell dumpsys activity |find "mFocusedActivity&q ...

  2. 致萌新与不会用 NOI Linux 的 OIer

    全文绝大部分转载自:这篇好文章啊. 目录 1:GUIDE 2:Gedit 原文 打开 编译运行 3.Vim 3-1:这东西咋开啊 3-2:这东西咋用啊 4.编译与运行 5.调试 6.CSP竞赛中编写代 ...

  3. uniapp微信小程序canvas绘图插入网络图片不显示

    网络图片缓存 在uni中wx可以用uni代替 无区别: 先把要插入的网络图片缓存(getImageInfo); let context = uni.createCanvasContext('first ...

  4. C++ stringstream 实现字符与数字之间的转换

    c++中利用srtingstream可以将数字转为字符串,或者将字符串转为数字: 首先将double型数字串转成了string: stringnum2string(double *a,int n) { ...

  5. axios网络封装模块

    功能特点 在浏览器中发送XMLHttpRequests请求 在node.js总发送http请求 支持Promise API 拦截请求和相应 转换请求和响应数据 axios请求方式 支持多种请求方式 a ...

  6. svn“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方案

    今天SVN提交代码遇到"Previous operation has not finished; run 'cleanup' if it was interrupted"报错,&q ...

  7. Tim Urban:如何选择真正适合你的职业?

    Wait But Why是一个专注于写长博客的网站,Tim Urban是网站的创始人之一.Tim Urban专注于写长论文,与时下的轻度阅读完全背道而驰,文章动辄几千甚至上万字,但令人吃惊的是却拥有惊 ...

  8. shell编程之awk

    awk是一种用于处理数据和生成报告的编程语言 awk可以在命令行中进行一些简单的操作,也可以被写成脚本来处理较大的应用问题 awk与grep.sed结合使用,将使shell编程更加容易 awk工作模式 ...

  9. css 实现换肤几种方式

    说起换肤功能,前端肯定不陌生,其实就是颜色值的更换,实现方式有很多,也各有优缺点 一.可供选择的换肤 对于只提供几种主题方案,让用户来选择的,一般就简单粗暴的写多套主题 一个全局class控制样式切换 ...

  10. 13Linux之磁盘管理

    13Linux之磁盘管理 目录 13Linux之磁盘管理 13 磁盘管理 13.1 两种分区格式 13.1.1 磁盘命名 13.1.2 mbr 13.1.3 gpt 13.2 制作文件系统并且挂载 1 ...