Object Pool Design Pattern in PHP

Object pool pattern is a software creational design pattern which is used in situations where the cost of initializing a class instance is high. It can offer a significant performance boost.

Object Pool Design Pattern UML

Object pool (resource pool) manages instantiated classes. Client code has access to the pool and can then use it to avoid creating new objects by going through the pool to find the one that has already been instantiated. When the pool is empty it will create new objects. Depending on the wanted implementation and functionality the pool can be also set to limit the number of instantiated classes.

PHP Example of Object Pool

<?php

class ObjectPool
{
    /** @var array Instances of reusable objects */
    private $instances = [];

    /**
     * Get object from instances.
     *
     * @param string $key Key for retrieving the instance.
     *
     * @return ReusableObject
     */
    public function get($key)
    {
        return $this->instances[$key];
    }

    /**
     * Add object to list of instances.
     *
     * @param ReusableObject
     */
     public function add($object, $key)
     {
         $this->instances[$key] = $object;
     }
}

class ReusableObject
{
    /**
     * Do something.
     */
    public function doSomething()
    {
        // ...
    }
}

// Client code
$pool = new ObjectPool();
$reusableObject = new ReusableObject();
$pool->add($reusableObject, 'reusable_object_key')

$reusableObject = $pool->get('reusable_object_key');
$reusableObject->doSomething();

More Practical Examples

Practical example can be also a managing a conference with call for papers. When the call for paper is issued, speakers propose their talks and the managers decide which speakers to invite. Speakers get assigned to talk sessions. If a speaker cancels the talk, the talk session becomes available for the next speaker.

Most of the time keep all reusable objects that are not currently in use in the same object pool so that they can be managed by one coherent policy. To achieve this, the reusable pool class is designed to be a singleton class.

Open Source PHP Implementations

Dependency injection container can also use object pool together with some other design patterns.

See Also

GitHub OctocatFound a typo? Something wrong with this content? Just fork and edit it.

Content of this work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. Code snippets in examples are published under the CC0 1.0 Universal (CC0 1.0). Thanks to all the contributors.