Files
DesignPatternsPHP/Structural/Flyweight/FlyweightFactory.php
2019-08-17 21:58:04 +02:00

31 lines
685 B
PHP

<?php
declare(strict_types=1);
namespace DesignPatterns\Structural\Flyweight;
/**
* A factory manages shared flyweights. Clients should not instantiate them directly,
* but let the factory take care of returning existing objects or creating new ones.
*/
class FlyweightFactory implements \Countable
{
/**
* @var CharacterFlyweight[]
*/
private $pool = [];
public function get(string $name): CharacterFlyweight
{
if (!isset($this->pool[$name])) {
$this->pool[$name] = new CharacterFlyweight($name);
}
return $this->pool[$name];
}
public function count(): int
{
return count($this->pool);
}
}