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: object-pool/README.md
+33-11
Original file line number
Diff line number
Diff line change
@@ -10,28 +10,31 @@ tags:
10
10
---
11
11
12
12
## Intent
13
-
When objects are expensive to create and they are needed only for
14
-
short periods of time it is advantageous to utilize the Object Pool pattern.
15
-
The Object Pool provides a cache for instantiated objects tracking which ones
16
-
are in use and which are available.
13
+
14
+
When objects are expensive to create and they are needed only for short periods of time it is
15
+
advantageous to utilize the Object Pool pattern. The Object Pool provides a cache for instantiated
16
+
objects tracking which ones are in use and which are available.
17
17
18
18
## Explanation
19
19
20
20
Real world example
21
21
22
-
> In our war game we need to use oliphaunts, massive and mythic beasts, but the problem is that they are extremely expensive to create. The solution is to create a pool of them, track which ones are in-use, and instead of disposing them re-use the instances.
22
+
> In our war game we need to use oliphaunts, massive and mythic beasts, but the problem is that they
23
+
> are extremely expensive to create. The solution is to create a pool of them, track which ones are
24
+
> in-use, and instead of disposing them re-use the instances.
23
25
24
26
In plain words
25
27
26
28
> Object Pool manages a set of instances instead of creating and destroying them on demand.
27
29
28
30
Wikipedia says
29
31
30
-
> The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand.
32
+
> The object pool pattern is a software creational design pattern that uses a set of initialized
33
+
> objects kept ready to use – a "pool" – rather than allocating and destroying them on demand.
31
34
32
35
**Programmatic Example**
33
36
34
-
Here's the basic Oliphaunt class. These are very expensive to create.
37
+
Here's the basic `Oliphaunt` class. These giants are very expensive to create.
35
38
36
39
```java
37
40
publicclassOliphaunt {
@@ -60,7 +63,7 @@ public class Oliphaunt {
60
63
}
61
64
```
62
65
63
-
Next we present the Object Pool and more specifically Oliphaunt Pool.
66
+
Next we present the `ObjectPool`and more specifically `OliphauntPool`.
64
67
65
68
```java
66
69
publicabstractclassObjectPool<T> {
@@ -100,7 +103,7 @@ public class OliphauntPool extends ObjectPool<Oliphaunt> {
100
103
}
101
104
```
102
105
103
-
And finally here's how we utilize the pool.
106
+
Finally, here's how we utilize the pool.
104
107
105
108
```java
106
109
var pool =newOliphauntPool();
@@ -113,11 +116,30 @@ And finally here's how we utilize the pool.
113
116
var oliphaunt5 = pool.checkOut();
114
117
```
115
118
119
+
Program output:
120
+
121
+
```
122
+
Pool available=0 inUse=0
123
+
Checked out Oliphaunt id=1
124
+
Pool available=0 inUse=1
125
+
Checked out Oliphaunt id=2
126
+
Checked out Oliphaunt id=3
127
+
Pool available=0 inUse=3
128
+
Checking in Oliphaunt id=1
129
+
Checking in Oliphaunt id=2
130
+
Pool available=2 inUse=1
131
+
Checked out Oliphaunt id=2
132
+
Checked out Oliphaunt id=1
133
+
Pool available=0 inUse=3
134
+
```
135
+
116
136
## Class diagram
137
+
117
138

118
139
119
140
## Applicability
141
+
120
142
Use the Object Pool pattern when
121
143
122
-
* The objects are expensive to create (allocation cost)
123
-
* You need a large number of short-lived objects (memory fragmentation)
144
+
* The objects are expensive to create (allocation cost).
145
+
* You need a large number of short-lived objects (memory fragmentation).
Copy file name to clipboardExpand all lines: observer/README.md
+40-24
Original file line number
Diff line number
Diff line change
@@ -10,29 +10,35 @@ tags:
10
10
---
11
11
12
12
## Also known as
13
+
13
14
Dependents, Publish-Subscribe
14
15
15
16
## Intent
16
-
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified
17
-
and updated automatically.
17
+
18
+
Define a one-to-many dependency between objects so that when one object changes state, all its
19
+
dependents are notified and updated automatically.
18
20
19
21
## Explanation
20
22
21
23
Real world example
22
24
23
-
> 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.
25
+
> In a land far away lives the races of hobbits and orcs. Both of them are mostly outdoors so they
26
+
> closely follow the changes in weather. One could say that they are constantly observing the
27
+
> weather.
24
28
25
29
In plain words
26
30
27
31
> Register as an observer to receive state changes in the object.
28
32
29
33
Wikipedia says
30
34
31
-
> 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.
35
+
> The observer pattern is a software design pattern in which an object, called the subject,
36
+
> maintains a list of its dependents, called observers, and notifies them automatically of any state
37
+
> changes, usually by calling one of their methods.
32
38
33
39
**Programmatic Example**
34
40
35
-
Let's first introduce the weather observer interface and our races, orcs and hobbits.
41
+
Let's first introduce the `WeatherObserver`interface and our races, `Orcs` and `Hobbits`.
36
42
37
43
```java
38
44
publicinterfaceWeatherObserver {
@@ -58,11 +64,12 @@ public class Hobbits implements WeatherObserver {
58
64
publicvoidupdate(WeatherTypecurrentWeather) {
59
65
switch (currentWeather) {
60
66
LOGGER.info("The hobbits are facing "+ currentWeather.getDescription() +" weather now");
67
+
}
61
68
}
62
69
}
63
70
```
64
71
65
-
Then here's the weather that is constantly changing.
72
+
Then here's the `Weather` that is constantly changing.
66
73
67
74
```java
68
75
publicclassWeather {
@@ -109,38 +116,47 @@ Here's the full example in action.
109
116
var weather =newWeather();
110
117
weather.addObserver(newOrcs());
111
118
weather.addObserver(newHobbits());
112
-
113
119
weather.timePasses();
114
-
// The weather changed to rainy.
115
-
// The orcs are facing rainy weather now
116
-
// The hobbits are facing rainy weather now
117
120
weather.timePasses();
118
-
// The weather changed to windy.
119
-
// The orcs are facing windy weather now
120
-
// The hobbits are facing windy weather now
121
121
weather.timePasses();
122
-
// The weather changed to cold.
123
-
// The orcs are facing cold weather now
124
-
// The hobbits are facing cold weather now
125
122
weather.timePasses();
126
-
// The weather changed to sunny.
127
-
// The orcs are facing sunny weather now
128
-
// The hobbits are facing sunny weather now
123
+
```
124
+
125
+
Program output:
126
+
127
+
```
128
+
The weather changed to rainy.
129
+
The orcs are facing rainy weather now
130
+
The hobbits are facing rainy weather now
131
+
The weather changed to windy.
132
+
The orcs are facing windy weather now
133
+
The hobbits are facing windy weather now
134
+
The weather changed to cold.
135
+
The orcs are facing cold weather now
136
+
The hobbits are facing cold weather now
137
+
The weather changed to sunny.
138
+
The orcs are facing sunny weather now
139
+
The hobbits are facing sunny weather now
129
140
```
130
141
131
142
## Class diagram
143
+
132
144

133
145
134
146
## Applicability
135
-
Use the Observer pattern in any of the following situations
136
147
137
-
*When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently
138
-
*When a change to one object requires changing others, and you don't know how many objects need to be changed
139
-
* 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
148
+
Use the Observer pattern in any of the following situations:
149
+
150
+
* When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in
151
+
separate objects lets you vary and reuse them independently.
152
+
* When a change to one object requires changing others, and you don't know how many objects need to
153
+
be changed.
154
+
* When an object should be able to notify other objects without making assumptions about who these
155
+
objects are. In other words, you don't want these objects tightly coupled.
140
156
141
157
## Typical Use Case
142
158
143
-
*Changing in one object leads to a change in other objects
159
+
* Changing in one object leads to a change in other objects.
0 commit comments