PHP7 Flyweight

This commit is contained in:
Dominik Liebler
2016-09-23 10:34:53 +02:00
parent de196765cf
commit 2e287acdf7
7 changed files with 343 additions and 336 deletions

View File

@@ -16,22 +16,16 @@ class CharacterFlyweight implements FlyweightInterface
*/ */
private $name; private $name;
/** public function __construct(string $name)
* @param string $name
*/
public function __construct($name)
{ {
$this->name = $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.
*
* @param string $font
*/
public function draw($font)
{ {
print_r("Character {$this->name} printed $font \n"); // 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);
} }
} }

View File

@@ -3,38 +3,26 @@
namespace DesignPatterns\Structural\Flyweight; namespace DesignPatterns\Structural\Flyweight;
/** /**
* A factory manages shared flyweights. Clients shouldn't instaniate them directly, * A factory manages shared flyweights. Clients should not instantiate them directly,
* but let the factory take care of returning existing objects or creating new ones. * but let the factory take care of returning existing objects or creating new ones.
*/ */
class FlyweightFactory class FlyweightFactory implements \Countable
{ {
/** /**
* Associative store for flyweight objects. * @var CharacterFlyweight[]
*
* @var array
*/ */
private $pool = array(); private $pool = [];
/** public function get(string $name): CharacterFlyweight
* Magic getter.
*
* @param string $name
*
* @return Flyweight
*/
public function __get($name)
{ {
if (!array_key_exists($name, $this->pool)) { if (!isset($this->pool[$name])) {
$this->pool[$name] = new CharacterFlyweight($name); $this->pool[$name] = new CharacterFlyweight($name);
} }
return $this->pool[$name]; return $this->pool[$name];
} }
/** public function count(): int
* @return int
*/
public function totalNumber()
{ {
return count($this->pool); return count($this->pool);
} }

View File

@@ -2,13 +2,7 @@
namespace DesignPatterns\Structural\Flyweight; namespace DesignPatterns\Structural\Flyweight;
/**
* An interface through which flyweights can receive and act on extrinsic state.
*/
interface FlyweightInterface interface FlyweightInterface
{ {
/** public function render(string $extrinsicState): string;
* @param string $extrinsicState
*/
public function draw($extrinsicState);
} }

View File

@@ -4,33 +4,28 @@ namespace DesignPatterns\Structural\Flyweight\Tests;
use DesignPatterns\Structural\Flyweight\FlyweightFactory; use DesignPatterns\Structural\Flyweight\FlyweightFactory;
/**
* FlyweightTest demonstrates how a client would use the flyweight structure
* You don't have to change the code of your client.
*/
class FlyweightTest extends \PHPUnit_Framework_TestCase class FlyweightTest extends \PHPUnit_Framework_TestCase
{ {
private $characters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', private $characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ); 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
private $fonts = array('Arial', 'Times New Roman', 'Verdana', 'Helvetica'); private $fonts = ['Arial', 'Times New Roman', 'Verdana', 'Helvetica'];
// This is about the number of characters in a book of average length
private $numberOfCharacters = 300000;
public function testFlyweight() public function testFlyweight()
{ {
$factory = new FlyweightFactory(); $factory = new FlyweightFactory();
for ($i = 0; $i < $this->numberOfCharacters; $i++) { foreach ($this->characters as $char) {
$char = $this->characters[array_rand($this->characters)]; foreach ($this->fonts as $font) {
$font = $this->fonts[array_rand($this->fonts)]; $flyweight = $factory->get($char);
$flyweight = $factory->$char; $rendered = $flyweight->render($font);
// External state can be passed in like this:
// $flyweight->draw($font); $this->assertEquals(sprintf('Character %s with font %s', $char, $font), $rendered);
}
} }
// Flyweight pattern ensures that instances are shared // Flyweight pattern ensures that instances are shared
// instead of having hundreds of thousands of individual objects // instead of having hundreds of thousands of individual objects
$this->assertLessThanOrEqual($factory->totalNumber(), count($this->characters)); // there must be one instance for every char that has been reused for displaying in different fonts
$this->assertCount(count($this->characters), $factory);
} }
} }

View File

@@ -1,25 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Diagram> <Diagram>
<ID>PHP</ID> <ID>PHP</ID>
<OriginalElement>\DesignPatterns\Structural\Flyweight\FlyweightFactory</OriginalElement> <OriginalElement>\DesignPatterns\Structural\Flyweight\CharacterFlyweight</OriginalElement>
<nodes> <nodes>
<node x="14.0" y="109.0">\DesignPatterns\Structural\Flyweight\CharacterFlyweight</node> <node x="19.0" y="101.0">\DesignPatterns\Structural\Flyweight\CharacterFlyweight</node>
<node x="235.0" y="0.0">\DesignPatterns\Structural\Flyweight\FlyweightFactory</node> <node x="230.0" y="94.0">\DesignPatterns\Structural\Flyweight\FlyweightFactory</node>
<node x="-8.0" y="0.0">\DesignPatterns\Structural\Flyweight\FlyweightInterface</node> <node x="0.0" y="0.0">\DesignPatterns\Structural\Flyweight\FlyweightInterface</node>
</nodes> </nodes>
<notes /> <notes />
<edges> <edges>
<edge source="\DesignPatterns\Structural\Flyweight\CharacterFlyweight" target="\DesignPatterns\Structural\Flyweight\FlyweightInterface"> <edge source="\DesignPatterns\Structural\Flyweight\CharacterFlyweight" target="\DesignPatterns\Structural\Flyweight\FlyweightInterface">
<point x="0.0" y="-56.5" /> <point x="0.0" y="-37.0" />
<point x="0.0" y="29.5" /> <point x="0.0" y="25.5" />
</edge> </edge>
</edges> </edges>
<settings layout="Hierarchic Group" zoom="1.0" x="191.0" y="111.0" /> <settings layout="Hierarchic Group" zoom="1.0" x="76.0" y="91.0" />
<SelectedNodes /> <SelectedNodes />
<Categories> <Categories>
<Category>Fields</Category> <Category>Fields</Category>
<Category>Constants</Category> <Category>Constants</Category>
<Category>Constructors</Category>
<Category>Methods</Category> <Category>Methods</Category>
</Categories> </Categories>
<VISIBILITY>private</VISIBILITY> <VISIBILITY>private</VISIBILITY>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 35 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 80 KiB