diff --git a/Structural/Flyweight/CharacterFlyweight.php b/Structural/Flyweight/Character.php similarity index 94% rename from Structural/Flyweight/CharacterFlyweight.php rename to Structural/Flyweight/Character.php index 4ff5808..9441388 100644 --- a/Structural/Flyweight/CharacterFlyweight.php +++ b/Structural/Flyweight/Character.php @@ -7,7 +7,7 @@ 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 CharacterFlyweight implements FlyweightInterface +class Character implements Text { /** * Any state stored by the concrete flyweight must be independent of its context. diff --git a/Structural/Flyweight/FlyweightFactory.php b/Structural/Flyweight/FlyweightFactory.php deleted file mode 100644 index 25ab186..0000000 --- a/Structural/Flyweight/FlyweightFactory.php +++ /dev/null @@ -1,30 +0,0 @@ -pool[$name])) { - $this->pool[$name] = new CharacterFlyweight($name); - } - - return $this->pool[$name]; - } - - public function count(): int - { - return count($this->pool); - } -} diff --git a/Structural/Flyweight/README.rst b/Structural/Flyweight/README.rst index a5525aa..81c0697 100644 --- a/Structural/Flyweight/README.rst +++ b/Structural/Flyweight/README.rst @@ -20,21 +20,27 @@ Code You can also find this code on `GitHub`_ -FlyweightInterface.php +Text.php -.. literalinclude:: FlyweightInterface.php +.. literalinclude:: Text.php :language: php :linenos: -CharacterFlyweight.php +Word.php -.. literalinclude:: CharacterFlyweight.php +.. literalinclude:: Word.php :language: php :linenos: -FlyweightFactory.php +Character.php -.. literalinclude:: FlyweightFactory.php +.. literalinclude:: Character.php + :language: php + :linenos: + +TextFactory.php + +.. literalinclude:: TextFactory.php :language: php :linenos: diff --git a/Structural/Flyweight/Tests/FlyweightTest.php b/Structural/Flyweight/Tests/FlyweightTest.php index 1373107..6133150 100644 --- a/Structural/Flyweight/Tests/FlyweightTest.php +++ b/Structural/Flyweight/Tests/FlyweightTest.php @@ -3,7 +3,7 @@ declare(strict_types=1); namespace DesignPatterns\Structural\Flyweight\Tests; -use DesignPatterns\Structural\Flyweight\FlyweightFactory; +use DesignPatterns\Structural\Flyweight\TextFactory; use PHPUnit\Framework\TestCase; class FlyweightTest extends TestCase @@ -14,20 +14,29 @@ class FlyweightTest extends TestCase public function testFlyweight() { - $factory = new FlyweightFactory(); + $factory = new TextFactory(); - foreach ($this->characters as $char) { - foreach ($this->fonts as $font) { - $flyweight = $factory->get($char); - $rendered = $flyweight->render($font); + for ($i = 0; $i <= 10; $i++) { + foreach ($this->characters as $char) { + foreach ($this->fonts as $font) { + $flyweight = $factory->get($char); + $rendered = $flyweight->render($font); - $this->assertSame(sprintf('Character %s with font %s', $char, $font), $rendered); + $this->assertSame(sprintf('Character %s with font %s', $char, $font), $rendered); + } } } + foreach ($this->fonts as $word) { + $flyweight = $factory->get($word); + $rendered = $flyweight->render('foobar'); + + $this->assertSame(sprintf('Word %s with font foobar', $word), $rendered); + } + // Flyweight pattern ensures that instances are shared // instead of having hundreds of thousands of individual objects // there must be one instance for every char that has been reused for displaying in different fonts - $this->assertCount(count($this->characters), $factory); + $this->assertCount(count($this->characters) + count($this->fonts), $factory); } } diff --git a/Structural/Flyweight/FlyweightInterface.php b/Structural/Flyweight/Text.php similarity index 62% rename from Structural/Flyweight/FlyweightInterface.php rename to Structural/Flyweight/Text.php index fab729d..f7fbb5f 100644 --- a/Structural/Flyweight/FlyweightInterface.php +++ b/Structural/Flyweight/Text.php @@ -3,7 +3,10 @@ declare(strict_types=1); namespace DesignPatterns\Structural\Flyweight; -interface FlyweightInterface +/** + * This is the interface that all flyweights need to implement + */ +interface Text { public function render(string $extrinsicState): string; } diff --git a/Structural/Flyweight/TextFactory.php b/Structural/Flyweight/TextFactory.php new file mode 100644 index 0000000..c954d3a --- /dev/null +++ b/Structural/Flyweight/TextFactory.php @@ -0,0 +1,39 @@ +charPool[$name])) { + $this->charPool[$name] = $this->create($name); + } + + return $this->charPool[$name]; + } + + private function create(string $name): Text + { + if (strlen($name) == 1) { + return new Character($name); + } else { + return new Word($name); + } + } + + public function count(): int + { + return count($this->charPool); + } +} diff --git a/Structural/Flyweight/Word.php b/Structural/Flyweight/Word.php new file mode 100644 index 0000000..b8d6ab4 --- /dev/null +++ b/Structural/Flyweight/Word.php @@ -0,0 +1,21 @@ +name = $name; + } + + public function render(string $font): string + { + return sprintf('Word %s with font %s', $this->name, $font); + } +} diff --git a/Structural/Flyweight/uml/uml.png b/Structural/Flyweight/uml/uml.png index 26af3e3..78a78df 100644 Binary files a/Structural/Flyweight/uml/uml.png and b/Structural/Flyweight/uml/uml.png differ