Skip to content

Commit 2e287ac

Browse files
author
Dominik Liebler
committed
PHP7 Flyweight
1 parent de19676 commit 2e287ac

File tree

7 files changed

+343
-336
lines changed

7 files changed

+343
-336
lines changed

Structural/Flyweight/CharacterFlyweight.php

+6-12
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,16 @@ class CharacterFlyweight implements FlyweightInterface
1616
*/
1717
private $name;
1818

19-
/**
20-
* @param string $name
21-
*/
22-
public function __construct($name)
19+
public function __construct(string $name)
2320
{
2421
$this->name = $name;
2522
}
2623

27-
/**
28-
* Clients supply the context-dependent information that the flyweight needs to draw itself
29-
* For flyweights representing characters, extrinsic state usually contains e.g. the font.
30-
*
31-
* @param string $font
32-
*/
33-
public function draw($font)
24+
public function render(string $font): string
3425
{
35-
print_r("Character {$this->name} printed $font \n");
26+
// Clients supply the context-dependent information that the flyweight needs to draw itself
27+
// For flyweights representing characters, extrinsic state usually contains e.g. the font.
28+
29+
return sprintf('Character %s with font %s', $this->name, $font);
3630
}
3731
}

Structural/Flyweight/FlyweightFactory.php

+7-19
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,26 @@
33
namespace DesignPatterns\Structural\Flyweight;
44

55
/**
6-
* A factory manages shared flyweights. Clients shouldn't instaniate them directly,
6+
* A factory manages shared flyweights. Clients should not instantiate them directly,
77
* but let the factory take care of returning existing objects or creating new ones.
88
*/
9-
class FlyweightFactory
9+
class FlyweightFactory implements \Countable
1010
{
1111
/**
12-
* Associative store for flyweight objects.
13-
*
14-
* @var array
12+
* @var CharacterFlyweight[]
1513
*/
16-
private $pool = array();
14+
private $pool = [];
1715

18-
/**
19-
* Magic getter.
20-
*
21-
* @param string $name
22-
*
23-
* @return Flyweight
24-
*/
25-
public function __get($name)
16+
public function get(string $name): CharacterFlyweight
2617
{
27-
if (!array_key_exists($name, $this->pool)) {
18+
if (!isset($this->pool[$name])) {
2819
$this->pool[$name] = new CharacterFlyweight($name);
2920
}
3021

3122
return $this->pool[$name];
3223
}
3324

34-
/**
35-
* @return int
36-
*/
37-
public function totalNumber()
25+
public function count(): int
3826
{
3927
return count($this->pool);
4028
}

Structural/Flyweight/FlyweightInterface.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22

33
namespace DesignPatterns\Structural\Flyweight;
44

5-
/**
6-
* An interface through which flyweights can receive and act on extrinsic state.
7-
*/
85
interface FlyweightInterface
96
{
10-
/**
11-
* @param string $extrinsicState
12-
*/
13-
public function draw($extrinsicState);
7+
public function render(string $extrinsicState): string;
148
}

Structural/Flyweight/Tests/FlyweightTest.php

+12-17
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,28 @@
44

55
use DesignPatterns\Structural\Flyweight\FlyweightFactory;
66

7-
/**
8-
* FlyweightTest demonstrates how a client would use the flyweight structure
9-
* You don't have to change the code of your client.
10-
*/
117
class FlyweightTest extends \PHPUnit_Framework_TestCase
128
{
13-
private $characters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
14-
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', );
15-
private $fonts = array('Arial', 'Times New Roman', 'Verdana', 'Helvetica');
16-
17-
// This is about the number of characters in a book of average length
18-
private $numberOfCharacters = 300000;
9+
private $characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
10+
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
11+
private $fonts = ['Arial', 'Times New Roman', 'Verdana', 'Helvetica'];
1912

2013
public function testFlyweight()
2114
{
2215
$factory = new FlyweightFactory();
2316

24-
for ($i = 0; $i < $this->numberOfCharacters; $i++) {
25-
$char = $this->characters[array_rand($this->characters)];
26-
$font = $this->fonts[array_rand($this->fonts)];
27-
$flyweight = $factory->$char;
28-
// External state can be passed in like this:
29-
// $flyweight->draw($font);
17+
foreach ($this->characters as $char) {
18+
foreach ($this->fonts as $font) {
19+
$flyweight = $factory->get($char);
20+
$rendered = $flyweight->render($font);
21+
22+
$this->assertEquals(sprintf('Character %s with font %s', $char, $font), $rendered);
23+
}
3024
}
3125

3226
// Flyweight pattern ensures that instances are shared
3327
// instead of having hundreds of thousands of individual objects
34-
$this->assertLessThanOrEqual($factory->totalNumber(), count($this->characters));
28+
// there must be one instance for every char that has been reused for displaying in different fonts
29+
$this->assertCount(count($this->characters), $factory);
3530
}
3631
}

Structural/Flyweight/uml/Flyweight.uml

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Diagram>
33
<ID>PHP</ID>
4-
<OriginalElement>\DesignPatterns\Structural\Flyweight\FlyweightFactory</OriginalElement>
4+
<OriginalElement>\DesignPatterns\Structural\Flyweight\CharacterFlyweight</OriginalElement>
55
<nodes>
6-
<node x="14.0" y="109.0">\DesignPatterns\Structural\Flyweight\CharacterFlyweight</node>
7-
<node x="235.0" y="0.0">\DesignPatterns\Structural\Flyweight\FlyweightFactory</node>
8-
<node x="-8.0" y="0.0">\DesignPatterns\Structural\Flyweight\FlyweightInterface</node>
6+
<node x="19.0" y="101.0">\DesignPatterns\Structural\Flyweight\CharacterFlyweight</node>
7+
<node x="230.0" y="94.0">\DesignPatterns\Structural\Flyweight\FlyweightFactory</node>
8+
<node x="0.0" y="0.0">\DesignPatterns\Structural\Flyweight\FlyweightInterface</node>
99
</nodes>
1010
<notes />
1111
<edges>
1212
<edge source="\DesignPatterns\Structural\Flyweight\CharacterFlyweight" target="\DesignPatterns\Structural\Flyweight\FlyweightInterface">
13-
<point x="0.0" y="-56.5" />
14-
<point x="0.0" y="29.5" />
13+
<point x="0.0" y="-37.0" />
14+
<point x="0.0" y="25.5" />
1515
</edge>
1616
</edges>
17-
<settings layout="Hierarchic Group" zoom="1.0" x="191.0" y="111.0" />
17+
<settings layout="Hierarchic Group" zoom="1.0" x="76.0" y="91.0" />
1818
<SelectedNodes />
1919
<Categories>
2020
<Category>Fields</Category>
2121
<Category>Constants</Category>
22-
<Category>Constructors</Category>
2322
<Category>Methods</Category>
2423
</Categories>
2524
<VISIBILITY>private</VISIBILITY>

Structural/Flyweight/uml/uml.png

16.8 KB
Loading

Structural/Flyweight/uml/uml.svg

+310-273
Loading

0 commit comments

Comments
 (0)