Files
DesignPatternsPHP/Structural/Flyweight/Character.php
2021-10-15 13:00:17 -04:00

29 lines
901 B
PHP

<?php
declare(strict_types=1);
namespace DesignPatterns\Structural\Flyweight;
/**
* Implements the flyweight interface and adds storage for intrinsic state, if any.
* Instances of concrete flyweights are shared by means of a factory.
*/
class Character implements Text
{
/**
* Any state stored by the concrete flyweight must be independent of its context.
* For flyweights representing characters, this is usually the corresponding character code.
*/
public function __construct(private string $name)
{
}
public function render(string $extrinsicState): string
{
// Clients supply the context-dependent information that the flyweight needs to draw itself
// For flyweights representing characters, extrinsic state usually contains e.g. the font.
return sprintf('Character %s with font %s', $this->name, $extrinsicState);
}
}