Object Pool Pattern

Category : Design Patterns | Sub Category : Creational design patterns | By Prasad Bonam Last updated: 2023-07-09 07:51:18 Viewed : 301


Object Pool Pattern:

The Object Pool pattern is a creational design pattern that provides a pool of pre-initialized and reusable objects, known as the "pool," to be used by clients. It aims to improve performance and efficiency by reusing existing objects instead of creating new ones. This pattern is especially useful when the cost of creating new objects is high or when there is a limited availability of resources. Here is an example of implementing the Object Pool pattern in Java:

java
import java.util.ArrayList;
import java.util.List;
// Object to be pooled class PooledObject { // Additional properties and methods of the object // ... public void reset() { // Reset the objects state to its initial values // ... } } // Object Pool class ObjectPool { private List<PooledObject> pool; private int maxSize; public ObjectPool(int maxSize) { this.maxSize = maxSize; pool = new ArrayList<>(maxSize); // Initialize the pool with pre-allocated objects for (int i = 0; i < maxSize; i++) { pool.add(new PooledObject()); } } public PooledObject acquireObject() { if (pool.isEmpty()) { // Handle the case when the pool is empty or create new objects dynamically // ... } PooledObject object = pool.remove(pool.size() - 1); object.reset(); // Reset the objects state before providing it to the client return object; } public void releaseObject(PooledObject object) { if (pool.size() < maxSize) { pool.add(object); } } } // Client code public class Client { public static void main(String[] args) { ObjectPool pool = new ObjectPool(5); // Acquire objects from the pool PooledObject object1 = pool.acquireObject(); PooledObject object2 = pool.acquireObject(); // Use the acquired objects // ... // Release the objects back to the pool pool.releaseObject(object1); pool.releaseObject(object2); } }

In this example:

  • The PooledObject class represents the object that will be pooled. It contains additional properties and methods specific to the objects functionality.
  • The ObjectPool class manages the pool of PooledObject instances. It initializes the pool with a specified maximum size and pre-allocates the objects.
  • The acquireObject() method is used by clients to acquire an object from the pool. If the pool is empty, you can handle the case by either creating new objects dynamically or waiting until an object becomes available.
  • The releaseObject() method allows clients to release an object back into the pool for reuse. The released object can be added to the pool if the pool has not reached its maximum size.
  • The Client class demonstrates how to use the object pool. It creates an ObjectPool instance with a maximum pool size of 5. It acquires objects from the pool, uses them as needed, and releases them back to the pool.

By using the Object Pool pattern, you can reduce the overhead of creating and destroying objects, leading to improved performance and resource utilization. It is particularly useful when creating new objects is an expensive operation or when the availability of resources is limited.

Search
Related Articles

Leave a Comment: