Update README.md
This commit is contained in:
@ -10,16 +10,18 @@ tags:
|
||||
---
|
||||
|
||||
## Intent
|
||||
When objects are expensive to create and they are needed only for
|
||||
short periods of time it is advantageous to utilize the Object Pool pattern.
|
||||
The Object Pool provides a cache for instantiated objects tracking which ones
|
||||
are in use and which are available.
|
||||
|
||||
When objects are expensive to create and they are needed only for short periods of time it is
|
||||
advantageous to utilize the Object Pool pattern. The Object Pool provides a cache for instantiated
|
||||
objects tracking which ones are in use and which are available.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
||||
> 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.
|
||||
> 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.
|
||||
|
||||
In plain words
|
||||
|
||||
@ -27,11 +29,12 @@ In plain words
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> 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.
|
||||
> 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.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
Here's the basic Oliphaunt class. These are very expensive to create.
|
||||
Here's the basic `Oliphaunt` class. These giants are very expensive to create.
|
||||
|
||||
```java
|
||||
public class Oliphaunt {
|
||||
@ -60,7 +63,7 @@ public class Oliphaunt {
|
||||
}
|
||||
```
|
||||
|
||||
Next we present the Object Pool and more specifically Oliphaunt Pool.
|
||||
Next we present the `ObjectPool` and more specifically `OliphauntPool`.
|
||||
|
||||
```java
|
||||
public abstract class ObjectPool<T> {
|
||||
@ -100,7 +103,7 @@ public class OliphauntPool extends ObjectPool<Oliphaunt> {
|
||||
}
|
||||
```
|
||||
|
||||
And finally here's how we utilize the pool.
|
||||
Finally, here's how we utilize the pool.
|
||||
|
||||
```java
|
||||
var pool = new OliphauntPool();
|
||||
@ -113,11 +116,30 @@ And finally here's how we utilize the pool.
|
||||
var oliphaunt5 = pool.checkOut();
|
||||
```
|
||||
|
||||
Program output:
|
||||
|
||||
```
|
||||
Pool available=0 inUse=0
|
||||
Checked out Oliphaunt id=1
|
||||
Pool available=0 inUse=1
|
||||
Checked out Oliphaunt id=2
|
||||
Checked out Oliphaunt id=3
|
||||
Pool available=0 inUse=3
|
||||
Checking in Oliphaunt id=1
|
||||
Checking in Oliphaunt id=2
|
||||
Pool available=2 inUse=1
|
||||
Checked out Oliphaunt id=2
|
||||
Checked out Oliphaunt id=1
|
||||
Pool available=0 inUse=3
|
||||
```
|
||||
|
||||
## Class diagram
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
|
||||
Use the Object Pool pattern when
|
||||
|
||||
* The objects are expensive to create (allocation cost)
|
||||
* You need a large number of short-lived objects (memory fragmentation)
|
||||
* The objects are expensive to create (allocation cost).
|
||||
* You need a large number of short-lived objects (memory fragmentation).
|
||||
|
Reference in New Issue
Block a user