added flyweight pattern

This commit is contained in:
janukobytsch 2015-02-02 00:11:48 +01:00
parent 27b9c2b3cf
commit d97f9831d5
4 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,36 @@
<?php
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 {
/**
* 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;
/**
* Constructor.
* @param string $name
*/
public function __construct($name) {
$this->name = $name;
}
/**
* 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");
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace DesignPatterns\Structural\Flyweight;
use DesignPatterns\Structural\Flyweight\CharacterFlyweight;
/**
* A factory manages shared flyweights. Clients shouldn't instaniate them directly,
* but let the factory take care of returning existing objects or creating new ones.
*/
class FlyweightFactory {
/**
* Associative store for flyweight objects
* @var Array
*/
private $pool = array();
/**
* Magic getter
* @param string $name
* @return Flyweight
*/
public function __get($name)
{
if (!array_key_exists((string) $name, $this->pool)) {
$this->pool[(string) $name] = new CharacterFlyweight((string) $name);
}
return $this->pool[(string) $name];
}
public function totalNumber()
{
return sizeof($this->pool);
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace DesignPatterns\Structural\Flyweight;
/**
* An interface through which flyweights can receive and act on extrinsic state
*/
interface FlyweightInterface {
public function draw($extrinsicState);
}

View File

@ -0,0 +1,36 @@
<?php
namespace DesignPatterns\Structural\Flyweight\Tests;
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
{
private $characters = array('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');
private $fonts = array('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()
{
$factory = new FlyweightFactory();
for ($i = 0; $i < $this->numberOfCharacters; $i++) {
$char = $this->characters[array_rand($this->characters)];
$font = $this->fonts[array_rand($this->fonts)];
$flyweight = $factory->$char;
// External state can be passed in like this:
// $flyweight->draw($font);
}
// Flyweight pattern ensures that instances are shared
// instead of having hundreds of thousands of individual objects
$this->assertLessThanOrEqual($factory->totalNumber(), sizeof($this->characters));
}
}