> 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
> Object Pool manages a set of instances instead of creating and destroying them on demand.
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.
**Programmatic Example**
Here's the basic Oliphaunt class. These are very expensive to create.
```java
public class Oliphaunt {
private static AtomicInteger counter = new AtomicInteger(0);
private final int id;
public Oliphaunt() {
id = counter.incrementAndGet();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public int getId() {
return id;
}
@Override
public String toString() {
return String.format("Oliphaunt id=%d", id);
}
}
```
Next we present the Object Pool and more specifically Oliphaunt Pool.