美文网首页
观察者模式

观察者模式

作者: 狐尼克朱迪 | 来源:发表于2016-10-21 15:14 被阅读0次

定义

观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会受到通知并自动更新

使用场景

  • 当一个抽象有两个方面,一个依赖于另一个。 将这些方面封装在单独的对象中,可以单独更改和重复使用它们
  • 当对一个对象的更改需要更改其他对象时,并且你不知道有多少对象需要更改
  • 当一个对象应该能够通知其他对象而不假设这些对象是谁。 换句话说,你不希望这些对象紧密耦合

例子

一个简单的天气预报的例子:

public interface WeatherObserver {
  void update(WeatherType currentWeather);
}

public enum WeatherType {
  SUNNY, RAINY, WINDY, COLD;
  @Override
  public String toString() {
    return this.name().toLowerCase();
  }
}

// 发布者
public class Weather {
  private WeatherType currentWeather;
  private List<WeatherObserver> observers;

  public Weather() {
    observers = new ArrayList<>();
    currentWeather = WeatherType.SUNNY;
  }

  public void addObserver(WeatherObserver obs) {
    observers.add(obs);
  }

  public void removeObserver(WeatherObserver obs) {
    observers.remove(obs);
  }

  public void timePasses() {
    WeatherType[] enumValues = WeatherType.values();
    currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
    System.out.println("The weather changed to " + currentWeather + ".");
    notifyObservers();
  }

  private void notifyObservers() {
    for (WeatherObserver obs : observers) {
      obs.update(currentWeather);
    }
  }
}


// 两个观察者
public class Hobbits implements WeatherObserver {

  @Override
  public void update(WeatherType currentWeather) {
    switch (currentWeather) {
      case COLD:
        System.out.println("The hobbits are shivering in the cold weather.");
        break;
      case RAINY:
        System.out.println("The hobbits look for cover from the rain.");
        break;
      case SUNNY:
        System.out.println("The happy hobbits bade in the warm sun.");
        break;
      case WINDY:
        System.out.println("The hobbits hold their hats tightly in the windy weather.");
        break;
      default:
        break;
    }
  }
}

public class Orcs implements WeatherObserver {

  @Override
  public void update(WeatherType currentWeather) {
    switch (currentWeather) {
      case COLD:
        System.out.println("The orcs are freezing cold.");
        break;
      case RAINY:
        System.out.println("The orcs are dripping wet.");
        break;
      case SUNNY:
        System.out.println("The sun hurts the orcs' eyes.");
        break;
      case WINDY:
        System.out.println("The orc smell almost vanishes in the wind.");
        break;
      default:
        break;
    }
  }
}

// 测试
public class App {
  public static void main(String[] args) {

    Weather weather = new Weather();
    weather.addObserver(new Orcs());
    weather.addObserver(new Hobbits());

    weather.timePasses();
    weather.timePasses();
  }
}

分析

观察者模式体现了为了交互对象之间的松耦合设计而努力的设计原则!

参考

iluwatar/java-design-patterns

相关文章

  • 11.9设计模式-观察者模式-详解

    设计模式-观察者模式 观察者模式详解 观察者模式在android中的实际运用 1.观察者模式详解 2.观察者模式在...

  • RxJava基础—观察者模式

    设计模式-观察者模式 观察者模式:观察者模式(有时又被称为发布(publish )-订阅(Subscribe)模式...

  • 前端面试考点之手写系列

    1、观察者模式 观察者模式(基于发布订阅模式) 有观察者,也有被观察者。 观察者需要放到被观察者列表中,被观察者的...

  • RxJava 原理篇

    一、框架思想 观察者模式观察者自下而上注入被观察者被观察者自上而下发射事件观察者模式 装饰器模式自上而下,被观察者...

  • 观察者模式

    观察者模式概念 观察者模式是对象的行为模式,又叫作发布-订阅(publish/subscrible)模式。 观察者...

  • 设计模式-观察者模式

    观察者模式介绍 观察者模式定义 观察者模式(又被称为发布-订阅(Publish/Subscribe)模式,属于行为...

  • 观察者模式

    观察者模式 观察者模式的定义 观察者模式(Observer Pattern)也叫做发布订阅模式(Publish/s...

  • iOS设计模式之观察者模式

    观察者模式 1、什么是观察者模式 观察者模式有时又被称为发布(publish)-订阅(Subscribe)模式、模...

  • 观察者模式和发布订阅模式区别

    观察者模式 所谓观察者模式,其实就是为了实现松耦合(loosely coupled)。 在观察者模式中,观察者需要...

  • RxJava(二)

    一、观察者模式 1.1、传统的观察者模式 1.2、RxJava 的观察者模式 区别传统的观察者模式是一个 Obse...

网友评论

      本文标题:观察者模式

      本文链接:https://www.haomeiwen.com/subject/sqccuttx.html