mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-28 10:40:17 +02:00
30 lines
660 B
PHP
30 lines
660 B
PHP
<?php
|
|
|
|
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);
|
|
}
|
|
}
|