You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: observer/README.md
+32-18Lines changed: 32 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,36 +3,33 @@ title: Observer
3
3
category: Behavioral
4
4
language: en
5
5
tag:
6
-
- Gang Of Four
7
-
- Reactive
6
+
- Decoupling
7
+
- Event-driven
8
+
- Gang Of Four
9
+
- Publish/subscribe
8
10
---
9
11
10
12
## Also known as
11
13
12
-
Dependents, Publish-Subscribe
14
+
* Dependents
13
15
14
16
## Intent
15
17
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.
18
19
19
20
## Explanation
20
21
21
22
Real-world example
22
23
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.
26
25
27
26
In plain words
28
27
29
28
> Register as an observer to receive state changes in the object.
30
29
31
30
Wikipedia says
32
31
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.
36
33
37
34
**Programmatic Example**
38
35
@@ -136,25 +133,42 @@ The hobbits are facing sunny weather now
136
133
137
134
## Class diagram
138
135
139
-

136
+

140
137
141
138
## Applicability
142
139
143
140
Use the Observer pattern in any of the following situations:
144
141
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.
* 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.
0 commit comments