add object pool pattern

This commit is contained in:
Jan Brecka
2014-03-24 16:51:49 +01:00
parent b0b0d4a1a4
commit d779b1e86a
7 changed files with 166 additions and 0 deletions

31
Pool/Pool.php Normal file
View File

@@ -0,0 +1,31 @@
<?php
namespace DesignPatterns\Pool;
class Pool
{
private $instances = array();
private $class;
public function __construct($class)
{
$this->class = $class;
}
public function get()
{
if (count($this->instances) > 0) {
return array_pop($this->instances);
}
return new $this->class();
}
public function dispose($instance)
{
$this->instances[] = $instance;
}
}