PHP design pattern of object pool pattern (pool) code examples complete (5)



Object pool mode is a kind of creative design mode that prepares a group of initialized object “pools” in advance, rather than creating or destroying them on demand. The client of the object pool will request an object from the object pool, and then use the returned object to perform related operations. When the client is finished, it will return the specific type of factory object to the object pool instead of destroying it.

In the case of high initial instance cost, high instantiation rate and insufficient available instances, object pool can greatly improve the performance. When the time cost of creating objects (especially through the network) is uncertain, the required objects can be obtained in the expected time through the object pool.

In any case, the object pool mode is very useful in creating time-consuming objects, such as creating database connections, socket connections, threads, and large graphic objects (such as fonts or bitmaps). In some cases, a simple object pool (no external resources, only memory) may not be efficient, or even damage performance.

UML class diagram

Official PHP advanced learning exchange community “click”The management has sorted out some materials, bat and other advanced knowledge systems are ready (relevant learning materials and written questions), and not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, tp6, laravel, yii2, redis, spool, swoft, Kafka, MySQL optimization, shell script, docker, microservice, nginx and other advanced knowledge points Advanced dry goods


  • WorkerPool.php
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])) {
            $this->freeWorkers[$key] = $worker;

    public function count(): int
        return count($this->occupiedWorkers) + count($this->freeWorkers);
  • StringReverseWorker.php
createdAt = new \DateTime();

    public function run(string $text)
        return strrev($text);


  • Tests/PoolTest.php
        $worker2 = $pool->get();

        $this->assertCount(2, $pool);
        $this->assertNotSame($worker1, $worker2);

    public function testCanGetSameInstanceTwiceWhenDisposingItFirst()
        $pool = new WorkerPool();
        $worker1 = $pool->get();
        $worker2 = $pool->get();

        $this->assertCount(1, $pool);
        $this->assertSame($worker1, $worker2);

PHP Internet architect’s growth path * “design patterns” Ultimate Guide

PHP Internet architect 50K growth Guide + general outline of industry problem solving (continuous update)

Interview 10 companies, get 9 offers, PHP interview questions in 2020

If you like my article and want to exchange and study with more senior developers, you can get more technical consultation and guidance related to the interview of large factories. Welcome to join usOur group – click here(group number 856460874).

If the content is good, I hope you can support and encourage me to like it, and welcome to communicate with me. In addition, if you have any questions, please put forward the content you want to see in the comments