1.5. Pool
1.5.1. Purpose
The object pool pattern is a software creational design pattern thatuses a set of initialized objects kept ready to use – a “pool” – ratherthan allocating and destroying them on demand. A client of the pool willrequest an object from the pool and perform operations on the returnedobject. When the client has finished, it returns the object, which is aspecific type of factory object, to the pool rather than destroying it.
Object pooling can offer a significant performance boost in situationswhere the cost of initializing a class instance is high, the rate ofinstantiation of a class is high, and the number of instances in use atany one time is low. The pooled object is obtained in predictable timewhen creation of the new objects (especially over network) may takevariable time.
However these benefits are mostly true for objects that are expensivewith respect to time, such as database connections, socket connections,threads and large graphic objects like fonts or bitmaps. In certainsituations, simple object pooling (that hold no external resources, butonly occupy memory) may not be efficient and could decrease performance.
1.5.2. UML Diagram
1.5.3. Code
You can also find this code on GitHub
WorkerPool.php
- <?php
- namespace DesignPatterns\Creational\Pool;
- class WorkerPool implements \Countable
- {
- /**
- * @var StringReverseWorker[]
- */
- private $occupiedWorkers = [];
- /**
- * @var StringReverseWorker[]
- */
- private $freeWorkers = [];
- public function get(): StringReverseWorker
- {
- if (count($this->freeWorkers) == 0) {
- $worker = new StringReverseWorker();
- } else {
- $worker = array_pop($this->freeWorkers);
- }
- $this->occupiedWorkers[spl_object_hash($worker)] = $worker;
- return $worker;
- }
- public function dispose(StringReverseWorker $worker)
- {
- $key = spl_object_hash($worker);
- if (isset($this->occupiedWorkers[$key])) {
- unset($this->occupiedWorkers[$key]);
- $this->freeWorkers[$key] = $worker;
- }
- }
- public function count(): int
- {
- return count($this->occupiedWorkers) + count($this->freeWorkers);
- }
- }
StringReverseWorker.php
- <?php
- namespace DesignPatterns\Creational\Pool;
- class StringReverseWorker
- {
- /**
- * @var \DateTime
- */
- private $createdAt;
- public function __construct()
- {
- $this->createdAt = new \DateTime();
- }
- public function run(string $text)
- {
- return strrev($text);
- }
- }
1.5.4. Test
Tests/PoolTest.php
- <?php
- namespace DesignPatterns\Creational\Pool\Tests;
- use DesignPatterns\Creational\Pool\WorkerPool;
- use PHPUnit\Framework\TestCase;
- class PoolTest extends TestCase
- {
- public function testCanGetNewInstancesWithGet()
- {
- $pool = new WorkerPool();
- $worker1 = $pool->get();
- $worker2 = $pool->get();
- $this->assertCount(2, $pool);
- $this->assertNotSame($worker1, $worker2);
- }
- public function testCanGetSameInstanceTwiceWhenDisposingItFirst()
- {
- $pool = new WorkerPool();
- $worker1 = $pool->get();
- $pool->dispose($worker1);
- $worker2 = $pool->get();
- $this->assertCount(1, $pool);
- $this->assertSame($worker1, $worker2);
- }
- }