Object Pool

Object Pool

Creational pattern

This pattern is widely used for working with large numbers of computationally expensive objects. This pattern is advantageous for those scenarios where our program needs these kinds of objects for a short time to discard them afterwards. This pattern allows us to reutilize objects to keep us from having to create them each time our application requires them, by storing previously created objects in order to be used later.

Object Pool design pattern structure
Object Pool design pattern structure

The components included in this pattern are:

  • IObjectPool: Interface which defines the basic structure of an Object Pool, it has the get and release methods which will be used for getting and releasing objects, respectively.
  • AbstractObjectPool: Abstract class which defines the default behavior of an Object Pool, this class can be used as a basis for creating a ConcreteObjectPool more quickly.
  • ConcreteObjectPool: It is the complete, and ready-to-be-used, implementation of an Object Pool.
  • IPoolableObject: Interface which should implement all the objects we want to manage through ObjectPool. With this interface we can define the basic structure all objects should have.
  • ConcretePoolableObject: These are the real objects to be managed by ObjectPool, they should inherit from IPoolableObject.
  • IObjectFactory: Interface which will define the structure of the ConcretePoolableObject factory.
  • ConcreteObjectFactory: Concrete factory which implements IObjectFactory for the creation of a ConcretePoolableObject.
Object Pool pattern sequence diagram.
Object Pool pattern sequence diagram.
  1. The client sends a request for an object to ConcreteObjectPool .
  2. ConcreteObjectPool checks if the required object is available, otherwise it will send the request for the creation of a new object to ConcreteObjectFactory.
  3. ConcreteObjectFactory creates a new object whose type is ConcretePoolableObject.
  4. ConcreteObjectPool returns the object to the client.
  5. The client uses the object.
  6. The client returns the object to ConcreteObjectPool.

Real-world example

By implementing the Object Pool design pattern, we are going to develop a multi-threaded application, which has to carry out many tasks simultaneously. The Object Pool will control how many processes can be executed at the same time, by setting a maximum limit in order to avoid running out of resources and to maintain an even performance throughout the execution, keeping the rest of the processes in a queue to be executed whenever it's allowed.

Discover how the Object Pool pattern can help us solve this problem
Discover how the Object Pool pattern can help us solve this problem

About this book

Introducción a la arquitectura de software

Everything you just saw in this article is only a small part of the book Introduction to design patterns, the most complete book of design patterns in Spanish, we cover 25 design patterns along with 25 real-world projects. Forget about learning design patterns with typical Internet examples of how to make pizza, animals, and geometric shapes. I invite you to see my book:

See book
All rights reserved ©