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
+49-15Lines changed: 49 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,36 +3,35 @@ title: Object Pool
3
3
category: Creational
4
4
language: en
5
5
tag:
6
-
- Game programming
7
-
- Performance
6
+
- Game programming
7
+
- Instantiation
8
+
- Memory management
9
+
- Performance
10
+
- Resource management
11
+
- Scalability
8
12
---
9
13
10
14
## Also known as
11
15
12
-
Resource Pool
16
+
*Resource Pool
13
17
14
18
## Intent
15
19
16
-
When objects are expensive to create and they are needed only for short periods of time it is
17
-
advantageous to utilize the Object Pool pattern. The Object Pool provides a cache for instantiated
18
-
objects tracking which ones are in use and which are available.
20
+
The Object Pool design pattern manages a pool of reusable objects, optimizing resource use by recycling objects rather than creating and destroying them repeatedly.
19
21
20
22
## Explanation
21
23
22
24
Real world example
23
25
24
-
> In our war game we need to use oliphaunts, massive and mythic beasts, but the problem is that they
25
-
> are extremely expensive to create. The solution is to create a pool of them, track which ones are
26
-
> in-use, and instead of disposing them re-use the instances.
26
+
> 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.
27
27
28
28
In plain words
29
29
30
30
> Object Pool manages a set of instances instead of creating and destroying them on demand.
31
31
32
32
Wikipedia says
33
33
34
-
> The object pool pattern is a software creational design pattern that uses a set of initialized
35
-
> objects kept ready to use – a "pool" – rather than allocating and destroying them on demand.
34
+
> 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.
36
35
37
36
**Programmatic Example**
38
37
@@ -65,7 +64,7 @@ public class Oliphaunt {
65
64
}
66
65
```
67
66
68
-
Next we present the `ObjectPool` and more specifically `OliphauntPool`.
67
+
Next, we present the `ObjectPool` and more specifically `OliphauntPool`.
* The objects are expensive to create (allocation cost).
147
-
* You need a large number of short-lived objects (memory fragmentation).
145
+
* You need to frequently create and destroy objects, leading to high resource allocation and deallocation costs.
146
+
* The objects are expensive to create and maintain (e.g., database connections, thread pools).
147
+
* A fixed number of objects need to be controlled, like in connection pooling.
148
+
* Object reuse can significantly improve system performance and resource management.
149
+
150
+
## Known Uses
151
+
152
+
* Database connection pooling in Java applications.
153
+
* Thread pooling in Java concurrent programming.
154
+
* Pooling of socket connections in network applications.
155
+
* Object pools in game development for frequently created and destroyed game objects.
156
+
157
+
## Consequences
158
+
159
+
Benefits:
160
+
161
+
* Improved Performance: Reduces the overhead of object creation and garbage collection.
162
+
* Resource Management: Controls the number of instances, reducing resource contention and limiting resource usage.
163
+
* Scalability: Allows the application to handle more requests by reusing a fixed number of objects.
164
+
165
+
Trade-offs:
166
+
167
+
* Complexity: Adds complexity to the codebase, requiring careful management of the pool.
168
+
* Thread Safety: Requires careful handling of concurrent access to the pool, introducing potential synchronization issues.
169
+
* Initialization Cost: Initial creation of the pool can be resource-intensive.
170
+
171
+
## Related Patterns
172
+
173
+
[Singleton](https://java-design-patterns.com/patterns/singleton/): Ensures a single instance of the pool is used, providing a global point of access.
174
+
[Flyweight](https://java-design-patterns.com/patterns/flyweight/): Shares fine-grained objects to reduce memory usage, complementing object pooling by managing object state efficiently.
175
+
[Factory Method](https://java-design-patterns.com/patterns/factory-method/): Often used to create objects within the pool, abstracting the instantiation process.
176
+
177
+
## Credits
178
+
179
+
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
180
+
*[Effective Java](https://amzn.to/4cGk2Jz)
181
+
*[Java Concurrency in Practice](https://amzn.to/4aRMruW)
0 commit comments