Skip to content

Commit 86b0520

Browse files
committed
docs: update observer
1 parent cf32821 commit 86b0520

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

observer/README.md

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,33 @@ title: Observer
33
category: Behavioral
44
language: en
55
tag:
6-
- Gang Of Four
7-
- Reactive
6+
- Decoupling
7+
- Event-driven
8+
- Gang Of Four
9+
- Publish/subscribe
810
---
911

1012
## Also known as
1113

12-
Dependents, Publish-Subscribe
14+
* Dependents
1315

1416
## Intent
1517

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

1920
## Explanation
2021

2122
Real-world example
2223

23-
> In a land far away live the races of hobbits and orcs. Both of them are mostly outdoors so they
24-
> closely follow the weather changes. One could say that they are constantly observing the
25-
> weather.
24+
> 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.
2625
2726
In plain words
2827

2928
> Register as an observer to receive state changes in the object.
3029
3130
Wikipedia says
3231

33-
> The observer pattern is a software design pattern in which an object, called the subject,
34-
> maintains a list of its dependents, called observers, and notifies them automatically of any state
35-
> changes, usually by calling one of their methods.
32+
> 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.
3633
3734
**Programmatic Example**
3835

@@ -136,25 +133,42 @@ The hobbits are facing sunny weather now
136133

137134
## Class diagram
138135

139-
![alt text](./etc/observer.png "Observer")
136+
![Observer](./etc/observer.png "Observer")
140137

141138
## Applicability
142139

143140
Use the Observer pattern in any of the following situations:
144141

145-
* When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in
146-
separate objects lets you vary and reuse them independently.
147-
* When a change to one object requires changing others, and you don't know how many objects need to
148-
be changed.
149-
* When an object should be able to notify other objects without making assumptions about who these
150-
objects are. In other words, you don't want these objects tightly coupled.
142+
* When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently.
143+
* When a change to one object requires changing others, and you don't know how many objects need to be changed.
144+
* 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.
151145

152146
## Known uses
153147

154148
* [java.util.Observer](http://docs.oracle.com/javase/8/docs/api/java/util/Observer.html)
155149
* [java.util.EventListener](http://docs.oracle.com/javase/8/docs/api/java/util/EventListener.html)
156150
* [javax.servlet.http.HttpSessionBindingListener](http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpSessionBindingListener.html)
157151
* [RxJava](https://github.com/ReactiveX/RxJava)
152+
* Model-View-Controller (MVC) frameworks.
153+
* Event handling systems.
154+
155+
## Consequences
156+
157+
Benefits:
158+
159+
* Promotes loose coupling between the subject and its observers.
160+
* Allows dynamic subscription and unsubscription of observers.
161+
162+
Trade-offs:
163+
164+
* Can lead to memory leaks if observers are not properly deregistered.
165+
* The order of notification is not specified, leading to potential unexpected behavior.
166+
* Potential for performance issues with a large number of observers.
167+
168+
## Related Patterns
169+
170+
* [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.
171+
* [Singleton](https://java-design-patterns.com/patterns/singleton/): Often used with the Observer pattern to ensure a single instance of the subject.
158172

159173
## Credits
160174

observer/src/main/java/com/iluwatar/observer/WeatherType.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
package com.iluwatar.observer;
2626

27-
/**
27+
import lombok.Getter; /**
2828
* WeatherType enumeration.
2929
*/
3030
public enum WeatherType {
@@ -34,16 +34,13 @@ public enum WeatherType {
3434
WINDY("Windy"),
3535
COLD("Cold");
3636

37+
@Getter
3738
private final String description;
3839

3940
WeatherType(String description) {
4041
this.description = description;
4142
}
4243

43-
public String getDescription() {
44-
return this.description;
45-
}
46-
4744
@Override
4845
public String toString() {
4946
return this.name().toLowerCase();

observer/src/test/java/com/iluwatar/observer/WeatherObserverTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,17 @@
2424
*/
2525
package com.iluwatar.observer;
2626

27-
import com.iluwatar.observer.utils.InMemoryAppender;
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
2828

29+
import com.iluwatar.observer.utils.InMemoryAppender;
2930
import java.util.Collection;
3031
import java.util.function.Supplier;
3132
import org.junit.jupiter.api.AfterEach;
3233
import org.junit.jupiter.api.BeforeEach;
33-
import org.junit.jupiter.api.Disabled;
3434
import org.junit.jupiter.api.TestInstance;
3535
import org.junit.jupiter.params.ParameterizedTest;
3636
import org.junit.jupiter.params.provider.MethodSource;
3737

38-
import static org.junit.jupiter.api.Assertions.assertEquals;
39-
4038
/**
4139
* Date: 12/27/15 - 11:44 AM
4240
* Weather Observer Tests

0 commit comments

Comments
 (0)