fixed eol and typecast

This commit is contained in:
janukobytsch
2015-02-02 15:36:32 +01:00
parent d97f9831d5
commit b2c034ec7f
4 changed files with 49 additions and 52 deletions

View File

@@ -6,31 +6,30 @@ namespace DesignPatterns\Structural\Flyweight;
* Implements the flyweight interface and adds storage for intrinsic state, if any. * Implements the flyweight interface and adds storage for intrinsic state, if any.
* Instances of concrete flyweights are shared by means of a factory. * 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. * Constructor.
* For flyweights representing characters, this is usually the corresponding character code. * @param string $name
* @var string */
*/ public function __construct($name) {
private $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");
}
/**
* 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

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

View File

@@ -5,8 +5,7 @@ namespace DesignPatterns\Structural\Flyweight;
/** /**
* An interface through which flyweights can receive and act on extrinsic state * An interface through which flyweights can receive and act on extrinsic state
*/ */
interface FlyweightInterface { interface FlyweightInterface
{
public function draw($extrinsicState); public function draw($extrinsicState);
} }

View File

@@ -10,23 +10,23 @@ use DesignPatterns\Structural\Flyweight\FlyweightFactory;
*/ */
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 = 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'); '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 = array('Arial', 'Times New Roman', 'Verdana', 'Helvetica');
// This is about the number of characters in a book of average length // This is about the number of characters in a book of average length
private $numberOfCharacters = 300000; private $numberOfCharacters = 300000;
public function testFlyweight() public function testFlyweight()
{ {
$factory = new FlyweightFactory(); $factory = new FlyweightFactory();
for ($i = 0; $i < $this->numberOfCharacters; $i++) { for ($i = 0; $i < $this->numberOfCharacters; $i++) {
$char = $this->characters[array_rand($this->characters)]; $char = $this->characters[array_rand($this->characters)];
$font = $this->fonts[array_rand($this->fonts)]; $font = $this->fonts[array_rand($this->fonts)];
$flyweight = $factory->$char; $flyweight = $factory->$char;
// External state can be passed in like this: // External state can be passed in like this:
// $flyweight->draw($font); // $flyweight->draw($font);
} }
// Flyweight pattern ensures that instances are shared // Flyweight pattern ensures that instances are shared