Browser docs


Also known as

Dependents, Publish-Subscribe


Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.


Real-world example

In a land far away live the races of hobbits and orcs. Both of them are mostly outdoors so they closely follow the weather changes. One could say that they are constantly observing the weather.

In plain words

Register as an observer to receive state changes in the object.

Wikipedia says

The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

Programmatic Example

Let’s first introduce the WeatherObserver interface and our races, Orcs and Hobbits.

 1public interface WeatherObserver {
 3  void update(WeatherType currentWeather);
 7public class Orcs implements WeatherObserver {
 9  @Override
10  public void update(WeatherType currentWeather) {
11"The orcs are facing " + currentWeather.getDescription() + " weather now");
12  }
16public class Hobbits implements WeatherObserver {
18  @Override
19  public void update(WeatherType currentWeather) {
20    switch (currentWeather) {
21"The hobbits are facing " + currentWeather.getDescription() + " weather now");
22    }
23  }

Then here’s the Weather that is constantly changing.

 2public class Weather {
 4  private WeatherType currentWeather;
 5  private final List<WeatherObserver> observers;
 7  public Weather() {
 8    observers = new ArrayList<>();
 9    currentWeather = WeatherType.SUNNY;
10  }
12  public void addObserver(WeatherObserver obs) {
13    observers.add(obs);
14  }
16  public void removeObserver(WeatherObserver obs) {
17    observers.remove(obs);
18  }
20  /**
21   * Makes time pass for weather.
22   */
23  public void timePasses() {
24    var enumValues = WeatherType.values();
25    currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
26"The weather changed to {}.", currentWeather);
27    notifyObservers();
28  }
30  private void notifyObservers() {
31    for (var obs : observers) {
32      obs.update(currentWeather);
33    }
34  }

Here’s the full example in action.

1    var weather = new Weather();
2    weather.addObserver(new Orcs());
3    weather.addObserver(new Hobbits());
4    weather.timePasses();
5    weather.timePasses();
6    weather.timePasses();
7    weather.timePasses();

Program output:

The weather changed to rainy.
The orcs are facing rainy weather now
The hobbits are facing rainy weather now
The weather changed to windy.
The orcs are facing windy weather now
The hobbits are facing windy weather now
The weather changed to cold.
The orcs are facing cold weather now
The hobbits are facing cold weather now
The weather changed to sunny.
The orcs are facing sunny weather now
The hobbits are facing sunny weather now

Class diagram

alt text


Use the Observer pattern in any of the following situations:

  • When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently.
  • When a change to one object requires changing others, and you don’t know how many objects need to be changed.
  • When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don’t want these objects tightly coupled.

Known uses