docs: update observer

This commit is contained in:
Ilkka Seppälä committed 2024-05-14 17:57:11 +03:00
1 parent cf3282149b
commit 86b0520f9d
3 files changed
+36 -27

No files matched your search

+32 -18
View File
@@ -3,26 +3,25 @@ title: Observer
category: Behavioral category: Behavioral
language: en language: en
tag: tag:
- Gang Of Four - Decoupling
- Reactive - Event-driven
- Gang Of Four
- Publish/subscribe
--- ---
## Also known as ## Also known as
Dependents, Publish-Subscribe * Dependents
## Intent ## Intent
Define a one-to-many dependency between objects so that when one object changes state, all its Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
dependents are notified and updated automatically.
## Explanation ## Explanation
Real-world example Real-world example
> In a land far away live the races of hobbits and orcs. Both of them are mostly outdoors so they > 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.
> closely follow the weather changes. One could say that they are constantly observing the
> weather.
In plain words In plain words
@@ -30,9 +29,7 @@ In plain words
Wikipedia says Wikipedia says
> The observer pattern is a software design pattern in which an object, called the subject, > 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.
> 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** **Programmatic Example**
@@ -136,18 +133,15 @@ The hobbits are facing sunny weather now
## Class diagram ## Class diagram
![alt text](./etc/observer.png "Observer") ![Observer](./etc/observer.png "Observer")
## Applicability ## Applicability
Use the Observer pattern in any of the following situations: 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 * When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently.
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 a change to one object requires changing others, and you don't know how many objects need to * 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.
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 ## Known uses
@@ -155,6 +149,26 @@ objects are. In other words, you don't want these objects tightly coupled.
* [java.util.EventListener](http://docs.oracle.com/javase/8/docs/api/java/util/EventListener.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) * [javax.servlet.http.HttpSessionBindingListener](http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpSessionBindingListener.html)
* [RxJava](https://github.com/ReactiveX/RxJava) * [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 ## Credits
@@ -24,7 +24,7 @@
*/ */
package com.iluwatar.observer; package com.iluwatar.observer;
/** import lombok.Getter; /**
* WeatherType enumeration. * WeatherType enumeration.
*/ */
public enum WeatherType { public enum WeatherType {
@@ -34,16 +34,13 @@ public enum WeatherType {
WINDY("Windy"), WINDY("Windy"),
COLD("Cold"); COLD("Cold");
@Getter
private final String description; private final String description;
WeatherType(String description) { WeatherType(String description) {
this.description = description; this.description = description;
} }
public String getDescription() {
return this.description;
}
@Override @Override
public String toString() { public String toString() {
return this.name().toLowerCase(); return this.name().toLowerCase();
@@ -24,19 +24,17 @@
*/ */
package com.iluwatar.observer; 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.Collection;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* Date: 12/27/15 - 11:44 AM * Date: 12/27/15 - 11:44 AM
* Weather Observer Tests * Weather Observer Tests