博客信息

观察者模式Observer(行为模式)

发布时间:『 2019-03-30 02:18』  博客类别:23种设计模式  阅读(761)
  • 术语

    Observer:观察者

    Subject:主题

  • 角色

  • Observer 观察者 百度 新浪 谷歌 Subject 气象局 WeatherData

UML类图

小李飞刀_设计模式


  • 案例

    需求:气象站数据更新推送问题

    • 出现前

    package com.javaxl.design.observer.before;

    /**
    * @author 小李飞刀
    * @site www.javaxl.com
    * @company
    * @create  2020-02-26 18:08
    */
    public class WeatherData {
      double temperature;
      double humidity;

      public WeatherData(double temperature, double humidity) {
          this.temperature = temperature;
          this.humidity = humidity;
      }

      public void getWeatherInfo() {
          System.out.println("当前温度:" + temperature + ",当前湿度:" + humidity);
      }

      public void change(double temperature, double humidity) {
          this.temperature = temperature;
          this.humidity = humidity;
      }
    }



    public class Baidu {
      private WeatherData weatherData;

      public Baidu(WeatherData weatherData) {
          this.weatherData = weatherData;
      }

      public void getWeatherInfo() {
          System.out.print("百度网站温馨提示===>");
          weatherData.getWeatherInfo();
      }
    }

    class Sina {
      private WeatherData weatherData;

      public Sina(WeatherData weatherData) {
          this.weatherData = weatherData;
      }

      public void getWeatherInfo() {
          System.out.print("新浪网站温馨提示===>");
          weatherData.getWeatherInfo();
      }
    }


    public class Client {
      public static void main(String[] args) {
          WeatherData weatherData = new WeatherData(30,20);
          Baidu baidu = new Baidu(weatherData);
          Sina sina = new Sina(weatherData);
          baidu.getWeatherInfo();
          sina.getWeatherInfo();
          weatherData.change(10,10);
          baidu.getWeatherInfo();
          sina.getWeatherInfo();
      }
    }

    由第三方(百度、新浪)主动获取最新天气信息,这种方案需要每个第三方主动定时获取最新天气数据,涉及多个第三方;

    • 出现后

package com.javaxl.design.observer.after;

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

/**
* @author 小李飞刀
* @site www.javaxl.com
* @company
* @create  2020-02-26 19:14
*/
public interface Subject {
  void addObserver(Observer observer);
  void removeObserver(Observer observer);
  void notifyObservers();
}

class WeatherData implements Subject{
  double temperature;
  double humidity;
  List<Observer> Observers = new ArrayList<>();

  public WeatherData(double temperature, double humidity) {
      this.temperature = temperature;
      this.humidity = humidity;
  }

  public void update(double temperature, double humidity) {
      this.temperature = temperature;
      this.humidity = humidity;
//       气象局数据一改变,马上通知接入的第三方/观察者
      notifyObservers();
  }

  @Override
  public void addObserver(Observer observer) {
      Observers.add(observer);
      observer.update(this.temperature,this.humidity);
  }

  @Override
  public void removeObserver(Observer observer) {
      Observers.remove(observer);
  }

  @Override
  public void notifyObservers() {
      for (Observer observer : Observers) {
          observer.update(this.temperature,this.humidity);
      }
  }
}


public interface Observer {
  void display();
  void update(double temperature, double humidity);
}

class Baidu implements Observer{
  double temperature;
  double humidity;

  @Override
  public void display() {
      System.out.println("百度温馨提示:当前温度:" + temperature + ",当前湿度:" + humidity);
  }

  @Override
  public void update(double temperature, double humidity) {
      this.temperature = temperature;
      this.humidity = humidity;
      this.display();
  }
}

class Sina implements Observer{
  double temperature;
  double humidity;

  @Override
  public void display() {
      System.out.println("新浪温馨提示:当前温度:" + temperature + ",当前湿度:" + humidity);
  }

  @Override
  public void update(double temperature, double humidity) {
      this.temperature = temperature;
      this.humidity = humidity;
      this.display();
  }
}

public class Client {
  public static void main(String[] args) {
      WeatherData weatherData = new WeatherData(30, 20);
      Baidu baidu = new Baidu();
      Sina sina = new Sina();
      weatherData.addObserver(baidu);
      weatherData.addObserver(sina);
      weatherData.update(10, 10);
      weatherData.removeObserver(baidu);
      weatherData.update(12, 12);
  }
}

由气象局主动通知第三方,天气数据发生了改变;并且,第三方的接入可以控制(增加、删除、通知);

  • 注意事项和细节

    集合的方式来管理用户(Observer),包括注册,移除和通知

  • 应用

    JDK源码中Observable类


over......


关键字:     设计模式  

备案号:湘ICP备19000029号

Copyright © 2018-2019 javaxl晓码阁 版权所有