Skip to content

Commit

Permalink
docs: update observer
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed May 14, 2024
1 parent cf32821 commit 86b0520
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
50 changes: 32 additions & 18 deletions observer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,33 @@ title: Observer
category: Behavioral
language: en
tag:
- Gang Of Four
- Reactive
- Decoupling
- Event-driven
- Gang Of Four
- Publish/subscribe
---

## Also known as

Dependents, Publish-Subscribe
* Dependents

## 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 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 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.
> 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**

Expand Down Expand Up @@ -136,25 +133,42 @@ The hobbits are facing sunny weather now

## Class diagram

![alt text](./etc/observer.png "Observer")
![Observer](./etc/observer.png "Observer")

## 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.
* 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

* [java.util.Observer](http://docs.oracle.com/javase/8/docs/api/java/util/Observer.html)
* [java.util.EventListener](http://docs.oracle.com/javase/8/docs/api/java/util/EventListener.html)
* [javax.servlet.http.HttpSessionBindingListener](http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpSessionBindingListener.html)
* [RxJava](https://github.com/ReactiveX/RxJava)
* Model-View-Controller (MVC) frameworks.
* Event handling systems.

## Consequences

Benefits:

* Promotes loose coupling between the subject and its observers.
* Allows dynamic subscription and unsubscription of observers.

Trade-offs:

* Can lead to memory leaks if observers are not properly deregistered.
* The order of notification is not specified, leading to potential unexpected behavior.
* Potential for performance issues with a large number of observers.

## Related Patterns

* [Mediator](https://java-design-patterns.com/patterns/mediator/): Encapsulates how a set of objects interact, which can be used to reduce the direct dependencies among objects.
* [Singleton](https://java-design-patterns.com/patterns/singleton/): Often used with the Observer pattern to ensure a single instance of the subject.

## Credits

Expand Down
7 changes: 2 additions & 5 deletions observer/src/main/java/com/iluwatar/observer/WeatherType.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
package com.iluwatar.observer;

/**
import lombok.Getter; /**
* WeatherType enumeration.
*/
public enum WeatherType {
Expand All @@ -34,16 +34,13 @@ public enum WeatherType {
WINDY("Windy"),
COLD("Cold");

@Getter
private final String description;

WeatherType(String description) {
this.description = description;
}

public String getDescription() {
return this.description;
}

@Override
public String toString() {
return this.name().toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,17 @@
*/
package com.iluwatar.observer;

import com.iluwatar.observer.utils.InMemoryAppender;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.iluwatar.observer.utils.InMemoryAppender;
import java.util.Collection;
import java.util.function.Supplier;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Date: 12/27/15 - 11:44 AM
* Weather Observer Tests
Expand Down

0 comments on commit 86b0520

Please sign in to comment.