Skip to content

Latest commit

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

layoutpattern
titleObserver
folderobserver
permalink/patterns/observer/
categoriesBehavioral
tags
Gang Of Four
Reactive

Also known as

Dependents, Publish-Subscribe

Intent

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

Explanation

Real world example

In a land far away lives the races of hobbits and orcs. Both of them are mostly outdoors so they closely follow the changes in weather. 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 weather observer interface and our races, orcs and hobbits.

publicinterfaceWeatherObserver {
voidupdate(inttemperature, inthumidity, intpressure);
}
publicclassStatisticDisplayimplementsObserver {
@Overridepublicvoidupdate(inttemperature, inthumidity, intpressure) {
System.out.println("Update StatisticDisplay display with new Parameters");
}
}
publicclassGeneralDisplayimplementsObserver {
@Overridepublicvoidupdate(inttemperature, inthumidity, intpressure) {
System.out.println("Update general display with new Parameters");
}
}

Then here's the weather that is constantly changing.

publicinterfaceSubject {
voidregisterObserver(Observerobserver);
voidremoveObserver(Observerobserver);
voidnotifyObserver();
}
publicclassWeatherDataimplementsSubject {
privateList<Observer> observers;
publicWeatherData() {
this.observers = newArrayList<>();
}
@OverridepublicvoidregisterObserver(Observerobserver) {
observers.add(observer);
}
@OverridepublicvoidremoveObserver(Observerobserver) {
observers.remove(observer);
}
@OverridepublicvoidnotifyObserver() {
observers.forEach(observer -> observer.update(getTemperature(), getHumidity(), getPressure()));
}
intgetTemperature(){
// some code here
}
intgetHumidity(){
// some code here
}
intgetPressure(){
// some code here
}
}

Here's the full example in action.

WeatherDataweatherData = newWeatherData();
weatherData.registerObserver(newGeneralDisplay());
weatherData.registerObserver(newStatisticDisplay());
weatherData.notifyObserver();

Class diagram

alt text

Applicability

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

Typical Use Case

  • Changing in one object leads to a change in other objects

Real world examples

Credits

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages