mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-10 08:54:03 +02:00
PHP7 Builder
This commit is contained in:
30
Creational/Pool/WorkerPool.php
Normal file
30
Creational/Pool/WorkerPool.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\Pool;
|
||||
|
||||
class Pool
|
||||
{
|
||||
private $instances = [];
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user