Update README.md
This commit is contained in:
@ -10,17 +10,21 @@ tags:
|
||||
---
|
||||
|
||||
## 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.
|
||||
|
||||
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 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
|
||||
|
||||
@ -28,11 +32,13 @@ In plain words
|
||||
|
||||
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.
|
||||
> 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.
|
||||
Let's first introduce the `WeatherObserver` interface and our races, `Orcs` and `Hobbits`.
|
||||
|
||||
```java
|
||||
public interface WeatherObserver {
|
||||
@ -58,11 +64,12 @@ public class Hobbits implements WeatherObserver {
|
||||
public void update(WeatherType currentWeather) {
|
||||
switch (currentWeather) {
|
||||
LOGGER.info("The hobbits are facing " + currentWeather.getDescription() + " weather now");
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Then here's the weather that is constantly changing.
|
||||
Then here's the `Weather` that is constantly changing.
|
||||
|
||||
```java
|
||||
public class Weather {
|
||||
@ -109,38 +116,47 @@ Here's the full example in action.
|
||||
var weather = new Weather();
|
||||
weather.addObserver(new Orcs());
|
||||
weather.addObserver(new Hobbits());
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
weather.timePasses();
|
||||
```
|
||||
|
||||
weather.timePasses();
|
||||
// The weather changed to rainy.
|
||||
// The orcs are facing rainy weather now
|
||||
// The hobbits are facing rainy weather now
|
||||
weather.timePasses();
|
||||
// The weather changed to windy.
|
||||
// The orcs are facing windy weather now
|
||||
// The hobbits are facing windy weather now
|
||||
weather.timePasses();
|
||||
// The weather changed to cold.
|
||||
// The orcs are facing cold weather now
|
||||
// The hobbits are facing cold weather now
|
||||
weather.timePasses();
|
||||
// The weather changed to sunny.
|
||||
// The orcs are facing sunny weather now
|
||||
// The hobbits are facing sunny weather now
|
||||
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
|
||||
|
||||

|
||||
|
||||
## 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
|
||||
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
|
||||
* Changing in one object leads to a change in other objects.
|
||||
|
||||
## Real world examples
|
||||
|
||||
|
@ -35,7 +35,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
*/
|
||||
public abstract class Observable<S extends Observable<S, O, A>, O extends Observer<S, O, A>, A> {
|
||||
|
||||
protected List<O> observers;
|
||||
protected final List<O> observers;
|
||||
|
||||
public Observable() {
|
||||
this.observers = new CopyOnWriteArrayList<>();
|
||||
|
Reference in New Issue
Block a user