mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-23 08:11:17 +02:00
@@ -11,8 +11,6 @@ class BookList implements \Countable
|
|||||||
if (isset($this->books[$bookNumberToGet])) {
|
if (isset($this->books[$bookNumberToGet])) {
|
||||||
return $this->books[$bookNumberToGet];
|
return $this->books[$bookNumberToGet];
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addBook(Book $book)
|
public function addBook(Book $book)
|
||||||
|
@@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\TemplateMethod;
|
namespace DesignPatterns\Behavioral\TemplateMethod;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
abstract class Journey
|
abstract class Journey
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\Builder;
|
namespace DesignPatterns\Creational\Builder;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
interface BuilderInterface
|
interface BuilderInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@@ -34,7 +34,7 @@ class FacadeTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Computer $facade
|
* @param Computer $facade
|
||||||
* @param OsInterface $os
|
* @param OsInterface $os
|
||||||
* @dataProvider getComputer
|
* @dataProvider getComputer
|
||||||
*/
|
*/
|
||||||
|
@@ -11,6 +11,7 @@ class CharacterFlyweight implements FlyweightInterface
|
|||||||
/**
|
/**
|
||||||
* Any state stored by the concrete flyweight must be independent of its context.
|
* Any state stored by the concrete flyweight must be independent of its context.
|
||||||
* For flyweights representing characters, this is usually the corresponding character code.
|
* For flyweights representing characters, this is usually the corresponding character code.
|
||||||
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $name;
|
private $name;
|
||||||
@@ -25,7 +26,7 @@ class CharacterFlyweight implements FlyweightInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Clients supply the context-dependent information that the flyweight needs to draw itself
|
* 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
|
* For flyweights representing characters, extrinsic state usually contains e.g. the font.
|
||||||
*
|
*
|
||||||
* @param string $font
|
* @param string $font
|
||||||
*/
|
*/
|
||||||
|
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Structural\Flyweight;
|
namespace DesignPatterns\Structural\Flyweight;
|
||||||
|
|
||||||
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.
|
||||||
@@ -11,16 +9,17 @@ use DesignPatterns\Structural\Flyweight\CharacterFlyweight;
|
|||||||
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)
|
||||||
@@ -28,7 +27,7 @@ class FlyweightFactory
|
|||||||
if (!array_key_exists($name, $this->pool)) {
|
if (!array_key_exists($name, $this->pool)) {
|
||||||
$this->pool[$name] = new CharacterFlyweight($name);
|
$this->pool[$name] = new CharacterFlyweight($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->pool[$name];
|
return $this->pool[$name];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
namespace DesignPatterns\Structural\Flyweight;
|
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
|
||||||
{
|
{
|
||||||
|
@@ -6,12 +6,12 @@ use DesignPatterns\Structural\Flyweight\FlyweightFactory;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* FlyweightTest demonstrates how a client would use the flyweight structure
|
* FlyweightTest demonstrates how a client would use the flyweight structure
|
||||||
* You don't have to change the code of your client
|
* 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 = 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
|
||||||
@@ -31,6 +31,6 @@ class FlyweightTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
// 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(), sizeof($this->characters));
|
$this->assertLessThanOrEqual($factory->totalNumber(), count($this->characters));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user