improved Flyweight

This commit is contained in:
Dominik Liebler
2019-08-19 17:47:02 +02:00
parent c00800f572
commit 2afed49abd
8 changed files with 94 additions and 46 deletions

View File

@@ -0,0 +1,32 @@
<?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.
*
* @var string
*/
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function render(string $font): 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, $font);
}
}