2015-05-29 12:11:16 +02:00
|
|
|
|
`Pool`__
|
|
|
|
|
========
|
2015-04-02 00:03:33 +02:00
|
|
|
|
|
2016-09-22 11:30:07 -05:00
|
|
|
|
Purpose
|
|
|
|
|
-------
|
|
|
|
|
|
2015-04-02 00:03:33 +02:00
|
|
|
|
The **object pool pattern** is a software creational design pattern that
|
|
|
|
|
uses a set of initialized objects kept ready to use – a "pool" – rather
|
|
|
|
|
than allocating and destroying them on demand. A client of the pool will
|
|
|
|
|
request an object from the pool and perform operations on the returned
|
|
|
|
|
object. When the client has finished, it returns the object, which is a
|
|
|
|
|
specific type of factory object, to the pool rather than destroying it.
|
|
|
|
|
|
|
|
|
|
Object pooling can offer a significant performance boost in situations
|
|
|
|
|
where the cost of initializing a class instance is high, the rate of
|
|
|
|
|
instantiation of a class is high, and the number of instances in use at
|
|
|
|
|
any one time is low. The pooled object is obtained in predictable time
|
|
|
|
|
when creation of the new objects (especially over network) may take
|
|
|
|
|
variable time.
|
|
|
|
|
|
|
|
|
|
However these benefits are mostly true for objects that are expensive
|
|
|
|
|
with respect to time, such as database connections, socket connections,
|
|
|
|
|
threads and large graphic objects like fonts or bitmaps. In certain
|
|
|
|
|
situations, simple object pooling (that hold no external resources, but
|
|
|
|
|
only occupy memory) may not be efficient and could decrease performance.
|
|
|
|
|
|
|
|
|
|
UML Diagram
|
|
|
|
|
-----------
|
|
|
|
|
|
|
|
|
|
.. image:: uml/uml.png
|
|
|
|
|
:alt: Alt Pool UML Diagram
|
|
|
|
|
:align: center
|
|
|
|
|
|
|
|
|
|
Code
|
|
|
|
|
----
|
|
|
|
|
|
2017-04-03 19:14:10 -06:00
|
|
|
|
You can also find this code on `GitHub`_
|
2015-04-02 00:03:33 +02:00
|
|
|
|
|
2016-09-22 14:16:12 +02:00
|
|
|
|
WorkerPool.php
|
2015-04-02 00:03:33 +02:00
|
|
|
|
|
2016-09-22 14:16:12 +02:00
|
|
|
|
.. literalinclude:: WorkerPool.php
|
2015-04-02 00:03:33 +02:00
|
|
|
|
:language: php
|
|
|
|
|
:linenos:
|
|
|
|
|
|
2016-09-22 14:16:12 +02:00
|
|
|
|
StringReverseWorker.php
|
2015-04-02 00:03:33 +02:00
|
|
|
|
|
2016-09-22 14:16:12 +02:00
|
|
|
|
.. literalinclude:: StringReverseWorker.php
|
2015-04-02 00:03:33 +02:00
|
|
|
|
:language: php
|
|
|
|
|
:linenos:
|
|
|
|
|
|
|
|
|
|
Test
|
|
|
|
|
----
|
|
|
|
|
|
2015-04-07 22:00:12 +02:00
|
|
|
|
Tests/PoolTest.php
|
2015-04-02 00:03:33 +02:00
|
|
|
|
|
2015-04-07 22:00:12 +02:00
|
|
|
|
.. literalinclude:: Tests/PoolTest.php
|
2015-04-02 00:03:33 +02:00
|
|
|
|
:language: php
|
|
|
|
|
:linenos:
|
|
|
|
|
|
2020-06-20 11:27:02 -03:00
|
|
|
|
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/main/Creational/Pool
|
2015-05-29 12:11:16 +02:00
|
|
|
|
.. __: http://en.wikipedia.org/wiki/Object_pool_pattern
|