Skip to content

Commit 2afed49

Browse files
author
Dominik Liebler
committed
improved Flyweight
1 parent c00800f commit 2afed49

File tree

8 files changed

+94
-46
lines changed

8 files changed

+94
-46
lines changed

Structural/Flyweight/CharacterFlyweight.php renamed to Structural/Flyweight/Character.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Implements the flyweight interface and adds storage for intrinsic state, if any.
88
* Instances of concrete flyweights are shared by means of a factory.
99
*/
10-
class CharacterFlyweight implements FlyweightInterface
10+
class Character implements Text
1111
{
1212
/**
1313
* Any state stored by the concrete flyweight must be independent of its context.

Structural/Flyweight/FlyweightFactory.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

Structural/Flyweight/README.rst

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,27 @@ Code
2020

2121
You can also find this code on `GitHub`_
2222

23-
FlyweightInterface.php
23+
Text.php
2424

25-
.. literalinclude:: FlyweightInterface.php
25+
.. literalinclude:: Text.php
2626
:language: php
2727
:linenos:
2828

29-
CharacterFlyweight.php
29+
Word.php
3030

31-
.. literalinclude:: CharacterFlyweight.php
31+
.. literalinclude:: Word.php
3232
:language: php
3333
:linenos:
3434

35-
FlyweightFactory.php
35+
Character.php
3636

37-
.. literalinclude:: FlyweightFactory.php
37+
.. literalinclude:: Character.php
38+
:language: php
39+
:linenos:
40+
41+
TextFactory.php
42+
43+
.. literalinclude:: TextFactory.php
3844
:language: php
3945
:linenos:
4046

Structural/Flyweight/Tests/FlyweightTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace DesignPatterns\Structural\Flyweight\Tests;
55

6-
use DesignPatterns\Structural\Flyweight\FlyweightFactory;
6+
use DesignPatterns\Structural\Flyweight\TextFactory;
77
use PHPUnit\Framework\TestCase;
88

99
class FlyweightTest extends TestCase
@@ -14,20 +14,29 @@ class FlyweightTest extends TestCase
1414

1515
public function testFlyweight()
1616
{
17-
$factory = new FlyweightFactory();
17+
$factory = new TextFactory();
1818

19-
foreach ($this->characters as $char) {
20-
foreach ($this->fonts as $font) {
21-
$flyweight = $factory->get($char);
22-
$rendered = $flyweight->render($font);
19+
for ($i = 0; $i <= 10; $i++) {
20+
foreach ($this->characters as $char) {
21+
foreach ($this->fonts as $font) {
22+
$flyweight = $factory->get($char);
23+
$rendered = $flyweight->render($font);
2324

24-
$this->assertSame(sprintf('Character %s with font %s', $char, $font), $rendered);
25+
$this->assertSame(sprintf('Character %s with font %s', $char, $font), $rendered);
26+
}
2527
}
2628
}
2729

30+
foreach ($this->fonts as $word) {
31+
$flyweight = $factory->get($word);
32+
$rendered = $flyweight->render('foobar');
33+
34+
$this->assertSame(sprintf('Word %s with font foobar', $word), $rendered);
35+
}
36+
2837
// Flyweight pattern ensures that instances are shared
2938
// instead of having hundreds of thousands of individual objects
3039
// there must be one instance for every char that has been reused for displaying in different fonts
31-
$this->assertCount(count($this->characters), $factory);
40+
$this->assertCount(count($this->characters) + count($this->fonts), $factory);
3241
}
3342
}

Structural/Flyweight/FlyweightInterface.php renamed to Structural/Flyweight/Text.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
namespace DesignPatterns\Structural\Flyweight;
55

6-
interface FlyweightInterface
6+
/**
7+
* This is the interface that all flyweights need to implement
8+
*/
9+
interface Text
710
{
811
public function render(string $extrinsicState): string;
912
}

Structural/Flyweight/TextFactory.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace DesignPatterns\Structural\Flyweight;
5+
6+
/**
7+
* A factory manages shared flyweights. Clients should not instantiate them directly,
8+
* but let the factory take care of returning existing objects or creating new ones.
9+
*/
10+
class TextFactory implements \Countable
11+
{
12+
/**
13+
* @var Text[]
14+
*/
15+
private $charPool = [];
16+
17+
public function get(string $name): Text
18+
{
19+
if (!isset($this->charPool[$name])) {
20+
$this->charPool[$name] = $this->create($name);
21+
}
22+
23+
return $this->charPool[$name];
24+
}
25+
26+
private function create(string $name): Text
27+
{
28+
if (strlen($name) == 1) {
29+
return new Character($name);
30+
} else {
31+
return new Word($name);
32+
}
33+
}
34+
35+
public function count(): int
36+
{
37+
return count($this->charPool);
38+
}
39+
}

Structural/Flyweight/Word.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\Flyweight;
4+
5+
class Word implements Text
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $name;
11+
12+
public function __construct(string $name)
13+
{
14+
$this->name = $name;
15+
}
16+
17+
public function render(string $font): string
18+
{
19+
return sprintf('Word %s with font %s', $this->name, $font);
20+
}
21+
}

Structural/Flyweight/uml/uml.png

4.58 KB
Loading

0 commit comments

Comments
 (0)