Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d779b1e

Browse files
committed
add object pool pattern
1 parent b0b0d4a commit d779b1e

File tree

7 files changed

+166
-0
lines changed

7 files changed

+166
-0
lines changed

Pool/Pool.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace DesignPatterns\Pool;
4+
5+
class Pool
6+
{
7+
8+
private $instances = array();
9+
private $class;
10+
11+
public function __construct($class)
12+
{
13+
$this->class = $class;
14+
}
15+
16+
public function get()
17+
{
18+
if (count($this->instances) > 0) {
19+
return array_pop($this->instances);
20+
}
21+
22+
return new $this->class();
23+
}
24+
25+
public function dispose($instance)
26+
{
27+
$this->instances[] = $instance;
28+
}
29+
30+
}
31+

Pool/Processor.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace DesignPatterns\Pool;
4+
5+
class Processor
6+
{
7+
8+
private $pool;
9+
private $processing = 0;
10+
private $maxProcesses = 3;
11+
private $waitingQueue = [];
12+
13+
public function __construct(Pool $pool)
14+
{
15+
$this->pool = $pool;
16+
}
17+
18+
public function process($image)
19+
{
20+
if ($this->processing++ < $this->maxProcesses) {
21+
$this->createWorker($image);
22+
} else {
23+
$this->pushToWaitingQueue($worker);
24+
}
25+
}
26+
27+
private function createWorker($image)
28+
{
29+
$worker = $this->pool->get();
30+
$worker->run($image, array($this, 'processDone'));
31+
}
32+
33+
public function processDone($worker)
34+
{
35+
$this->processing--;
36+
$this->pool->dispose($worker);
37+
38+
if (count($this->waitingQueue) > 0) {
39+
$this->createWorker($this->popFromWaitingQueue());
40+
}
41+
}
42+
43+
private function pushToWaitingQueue($image)
44+
{
45+
$this->waitingQueue[] = $image;
46+
}
47+
48+
private function popFromWaitingQueue()
49+
{
50+
return array_pop($this->waitingQueue);
51+
}
52+
53+
}
54+

Pool/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Pool
2+
====
3+
4+
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.
5+
6+
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.
7+
8+
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.
9+

Pool/Worker.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace DesignPatterns\Pool;
4+
5+
class Worker
6+
{
7+
8+
public function __construct()
9+
{
10+
// let say that constuctor does really expensive work...
11+
// for example creates "thread"
12+
}
13+
14+
public function run($image, array $callback)
15+
{
16+
// do something with $image...
17+
// and when it's done, execute callback
18+
call_user_func($callback, $this);
19+
}
20+
21+
}
22+

Pool/index.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use DesignPatterns\Pool\Pool;
4+
use DesignPatterns\Pool\Processor;
5+
6+
$pool = new Pool('DesignPatterns\Pool\Worker');
7+
$processor = new Processor($pool);
8+
9+
$processor->process('image1.jpg');
10+
$processor->process('image2.jpg');
11+
$processor->process('image3.jpg');
12+
$processor->process('image4.jpg');
13+
$processor->process('image5.jpg');
14+
$processor->process('image6.jpg');
15+
$processor->process('image7.jpg');
16+
$processor->process('image8.jpg');
17+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The patterns can be structured in roughly three different categories. Please cli
1818
* [FactoryMethod](FactoryMethod) [:notebook:](http://en.wikipedia.org/wiki/Factory_method_pattern)
1919
* [StaticFactory](StaticFactory)
2020
* [Prototype](Prototype) [:notebook:](http://en.wikipedia.org/wiki/Prototype_pattern)
21+
* [Pool](Prototype) [:notebook:](http://en.wikipedia.org/wiki/Object_pool_pattern)
2122
* [Singleton](Singleton) [:notebook:](http://en.wikipedia.org/wiki/Singleton_pattern) (is considered an anti-pattern! :no_entry:)
2223
* [Multiton](Multiton) (is considered an anti-pattern! :no_entry:)
2324

Tests/Pool/PoolTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace DesignPatterns\Tests\Pool;
4+
5+
use DesignPatterns\Pool\Pool;
6+
7+
class TestWorker
8+
{
9+
10+
public $id = 1;
11+
12+
}
13+
14+
class PoolTest extends \PHPUnit_Framework_TestCase
15+
{
16+
17+
public function testPool()
18+
{
19+
$pool = new Pool('DesignPatterns\Tests\Pool\TestWorker');
20+
$worker = $pool->get();
21+
22+
$this->assertEquals(1, $worker->id);
23+
24+
$worker->id = 5;
25+
$pool->dispose($worker);
26+
27+
$this->assertEquals(5, $pool->get()->id);
28+
$this->assertEquals(1, $pool->get()->id);
29+
}
30+
31+
}
32+

0 commit comments

Comments
 (0)