diff --git a/Structural/Flyweight/CharacterFlyweight.php b/Structural/Flyweight/CharacterFlyweight.php index 77d6b2d..43908c1 100644 --- a/Structural/Flyweight/CharacterFlyweight.php +++ b/Structural/Flyweight/CharacterFlyweight.php @@ -6,31 +6,30 @@ 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 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; - /** - * 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; + } - /** - * 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"); - } - -} \ No newline at end of file + /** + * 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"); + } +} diff --git a/Structural/Flyweight/FlyweightFactory.php b/Structural/Flyweight/FlyweightFactory.php index 4558300..5b17a43 100644 --- a/Structural/Flyweight/FlyweightFactory.php +++ b/Structural/Flyweight/FlyweightFactory.php @@ -8,30 +8,29 @@ 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 +class FlyweightFactory +{ + /** + * Associative store for flyweight objects * @var Array */ - private $pool = 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); + if (!array_key_exists($name, $this->pool)) { + $this->pool[$name] = new CharacterFlyweight($name); } - return $this->pool[(string) $name]; + return $this->pool[$name]; } public function totalNumber() { return sizeof($this->pool); } - -} \ No newline at end of file +} diff --git a/Structural/Flyweight/FlyweightInterface.php b/Structural/Flyweight/FlyweightInterface.php index cf43d57..e48bb8f 100644 --- a/Structural/Flyweight/FlyweightInterface.php +++ b/Structural/Flyweight/FlyweightInterface.php @@ -5,8 +5,7 @@ namespace DesignPatterns\Structural\Flyweight; /** * An interface through which flyweights can receive and act on extrinsic state */ -interface FlyweightInterface { - - public function draw($extrinsicState); - -} \ No newline at end of file +interface FlyweightInterface +{ + public function draw($extrinsicState); +} diff --git a/Structural/Flyweight/Tests/FlyweightTest.php b/Structural/Flyweight/Tests/FlyweightTest.php index ade3fde..90c3f3a 100644 --- a/Structural/Flyweight/Tests/FlyweightTest.php +++ b/Structural/Flyweight/Tests/FlyweightTest.php @@ -10,23 +10,23 @@ use DesignPatterns\Structural\Flyweight\FlyweightFactory; */ 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'); + 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; + // 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); + $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