it was created the Creational namespace and append its patterns

This commit is contained in:
Antonio Spinelli
2014-04-15 22:59:59 -03:00
parent 646e0e2fd9
commit 7bf6593e3f
53 changed files with 77 additions and 95 deletions

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\AbstractFactory;
namespace DesignPatterns\Creational\AbstractFactory;
/**
* class AbstractFactory

View File

@@ -1,10 +1,10 @@
<?php
namespace DesignPatterns\Tests\AbstractFactory;
namespace DesignPatterns\Creational\AbstractFactory;
use DesignPatterns\AbstractFactory\AbstractFactory;
use DesignPatterns\AbstractFactory\HtmlFactory;
use DesignPatterns\AbstractFactory\JsonFactory;
use DesignPatterns\Creational\AbstractFactory\AbstractFactory;
use DesignPatterns\Creational\AbstractFactory\HtmlFactory;
use DesignPatterns\Creational\AbstractFactory\JsonFactory;
/**
* AbstractFactoryTest tests concrete factories
@@ -34,7 +34,7 @@ class AbstractFactoryTest extends \PHPUnit_Framework_TestCase
$factory->createText('footnotes')
);
$this->assertContainsOnly('DesignPatterns\AbstractFactory\MediaInterface', $article);
$this->assertContainsOnly('DesignPatterns\Creational\AbstractFactory\MediaInterface', $article);
/* this is the time to look at the Builder pattern. This pattern
* helps you to create complex object like that article above with

View File

@@ -1,8 +1,8 @@
<?php
namespace DesignPatterns\AbstractFactory\Html;
namespace DesignPatterns\Creational\AbstractFactory\Html;
use DesignPatterns\AbstractFactory\Picture as BasePicture;
use DesignPatterns\Creational\AbstractFactory\Picture as BasePicture;
/**
* Class Picture

View File

@@ -1,8 +1,8 @@
<?php
namespace DesignPatterns\AbstractFactory\Html;
namespace DesignPatterns\Creational\AbstractFactory\Html;
use DesignPatterns\AbstractFactory\Text as BaseText;
use DesignPatterns\Creational\AbstractFactory\Text as BaseText;
/**
* Class Text

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\AbstractFactory;
namespace DesignPatterns\Creational\AbstractFactory;
/**
* Class HtmlFactory

View File

@@ -1,8 +1,8 @@
<?php
namespace DesignPatterns\AbstractFactory\Json;
namespace DesignPatterns\Creational\AbstractFactory\Json;
use DesignPatterns\AbstractFactory\Picture as BasePicture;
use DesignPatterns\Creational\AbstractFactory\Picture as BasePicture;
/**
* Class Picture

View File

@@ -1,8 +1,8 @@
<?php
namespace DesignPatterns\AbstractFactory\Json;
namespace DesignPatterns\Creational\AbstractFactory\Json;
use DesignPatterns\AbstractFactory\Text as BaseText;
use DesignPatterns\Creational\AbstractFactory\Text as BaseText;
/**
* Class Text

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\AbstractFactory;
namespace DesignPatterns\Creational\AbstractFactory;
/**
* Class JsonFactory

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\AbstractFactory;
namespace DesignPatterns\Creational\AbstractFactory;
/**
* Interface MediaInterface

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\AbstractFactory;
namespace DesignPatterns\Creational\AbstractFactory;
/**
* Class Picture

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\AbstractFactory;
namespace DesignPatterns\Creational\AbstractFactory;
/**
* Class Text

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Builder;
namespace DesignPatterns\Creational\Builder;
/**
* BikeBuilder builds bike

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Builder;
namespace DesignPatterns\Creational\Builder;
/**
*

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Builder;
namespace DesignPatterns\Creational\Builder;
/**
* CarBuilder builds car

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Builder;
namespace DesignPatterns\Creational\Builder;
/**
* Director is part of the builder pattern. It knows the interface of the builder

View File

@@ -1,11 +1,11 @@
<?php
namespace DesignPatterns\Tests\Builder;
namespace DesignPatterns\Creational\Builder;
use DesignPatterns\Builder\Director;
use DesignPatterns\Builder\CarBuilder;
use DesignPatterns\Builder\BikeBuilder;
use DesignPatterns\Builder\BuilderInterface;
use DesignPatterns\Creational\Builder\Director;
use DesignPatterns\Creational\Builder\CarBuilder;
use DesignPatterns\Creational\Builder\BikeBuilder;
use DesignPatterns\Creational\Builder\BuilderInterface;
/**
* DirectorTest tests the builder pattern
@@ -37,6 +37,6 @@ class DirectorTest extends \PHPUnit_Framework_TestCase
public function testBuild(BuilderInterface $builder)
{
$newVehicle = $this->director->build($builder);
$this->assertInstanceOf('DesignPatterns\Builder\Parts\Vehicle', $newVehicle);
$this->assertInstanceOf('DesignPatterns\Creational\Builder\Parts\Vehicle', $newVehicle);
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Builder\Parts;
namespace DesignPatterns\Creational\Builder\Parts;
/**
* Bike is a bike

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Builder\Parts;
namespace DesignPatterns\Creational\Builder\Parts;
/**
* Car is a car

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Builder\Parts;
namespace DesignPatterns\Creational\Builder\Parts;
/**
* Class Door

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Builder\Parts;
namespace DesignPatterns\Creational\Builder\Parts;
/**
* Class Engine

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Builder\Parts;
namespace DesignPatterns\Creational\Builder\Parts;
/**
* VehicleInterface is a contract for a vehicle

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Builder\Parts;
namespace DesignPatterns\Creational\Builder\Parts;
/**
* Class Wheel

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\FactoryMethod;
namespace DesignPatterns\Creational\FactoryMethod;
/**
* Bicycle is a bicycle

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\FactoryMethod;
namespace DesignPatterns\Creational\FactoryMethod;
/**
* class FactoryMethod

View File

@@ -1,10 +1,10 @@
<?php
namespace DesignPatterns\Tests\FactoryMethod;
namespace DesignPatterns\Creational\FactoryMethod;
use DesignPatterns\FactoryMethod\FactoryMethod;
use DesignPatterns\FactoryMethod\GermanFactory;
use DesignPatterns\FactoryMethod\ItalianFactory;
use DesignPatterns\Creational\FactoryMethod\FactoryMethod;
use DesignPatterns\Creational\FactoryMethod\GermanFactory;
use DesignPatterns\Creational\FactoryMethod\ItalianFactory;
/**
* FactoryMethodTest tests the factory method pattern
@@ -34,7 +34,7 @@ class FactoryMethodTest extends \PHPUnit_Framework_TestCase
// about the factory, all we know is it can produce vehicle
foreach ($this->type as $oneType) {
$vehicle = $shop->create($oneType);
$this->assertInstanceOf('DesignPatterns\FactoryMethod\VehicleInterface', $vehicle);
$this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\VehicleInterface', $vehicle);
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\FactoryMethod;
namespace DesignPatterns\Creational\FactoryMethod;
/**
* Ferrari is a italian car

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\FactoryMethod;
namespace DesignPatterns\Creational\FactoryMethod;
/**
* GermanFactory is a vehicle factory in Germany

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\FactoryMethod;
namespace DesignPatterns\Creational\FactoryMethod;
/**
* ItalianFactory is vehicle factory in Italy

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\FactoryMethod;
namespace DesignPatterns\Creational\FactoryMethod;
/**
* Porsche is a german car

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\FactoryMethod;
namespace DesignPatterns\Creational\FactoryMethod;
/**
* VehicleInterface is a contract for a vehicle

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns;
namespace DesignPatterns\Creational\Multiton;
/**
* class Multiton

30
Creational/Pool/Pool.php Normal file
View File

@@ -0,0 +1,30 @@
<?php
namespace DesignPatterns\Creational\Pool;
class Pool
{
private $instances = array();
private $class;
public function __construct($class)
{
$this->class = $class;
}
public function get()
{
if (count($this->instances) > 0) {
return array_pop($this->instances);
}
return new $this->class();
}
public function dispose($instance)
{
$this->instances[] = $instance;
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace DesignPatterns\Creational\Pool;
use DesignPatterns\Creational\Pool\Pool;
class TestWorker
{
public $id = 1;
}
class PoolTest extends \PHPUnit_Framework_TestCase
{
public function testPool()
{
$pool = new Pool('DesignPatterns\Creational\Pool\TestWorker');
$worker = $pool->get();
$this->assertEquals(1, $worker->id);
$worker->id = 5;
$pool->dispose($worker);
$this->assertEquals(5, $pool->get()->id);
$this->assertEquals(1, $pool->get()->id);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace DesignPatterns\Creational\Pool;
class Processor
{
private $pool;
private $processing = 0;
private $maxProcesses = 3;
private $waitingQueue = [];
public function __construct(Pool $pool)
{
$this->pool = $pool;
}
public function process($image)
{
if ($this->processing++ < $this->maxProcesses) {
$this->createWorker($image);
} else {
$this->pushToWaitingQueue($worker);
}
}
private function createWorker($image)
{
$worker = $this->pool->get();
$worker->run($image, array($this, 'processDone'));
}
public function processDone($worker)
{
$this->processing--;
$this->pool->dispose($worker);
if (count($this->waitingQueue) > 0) {
$this->createWorker($this->popFromWaitingQueue());
}
}
private function pushToWaitingQueue($image)
{
$this->waitingQueue[] = $image;
}
private function popFromWaitingQueue()
{
return array_pop($this->waitingQueue);
}
}

View File

@@ -0,0 +1,8 @@
Pool
====
The **object pool pattern** is a software creational design pattern that uses a set of initialized objects kept ready to use a "pool" rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished, it returns the object, which is a specific type of factory object, to the pool rather than destroying it.
Object pooling can offer a significant performance boost in situations where the cost of initializing a class instance is high, the rate of instantiation of a class is high, and the number of instances in use at any one time is low. The pooled object is obtained in predictable time when creation of the new objects (especially over network) may take variable time.
However these benefits are mostly true for objects that are expensive with respect to time, such as database connections, socket connections, threads and large graphic objects like fonts or bitmaps. In certain situations, simple object pooling (that hold no external resources, but only occupy memory) may not be efficient and could decrease performance.

View File

@@ -0,0 +1,21 @@
<?php
namespace DesignPatterns\Creational\Pool;
class Worker
{
public function __construct()
{
// let's say that constuctor does really expensive work...
// for example creates "thread"
}
public function run($image, array $callback)
{
// do something with $image...
// and when it's done, execute callback
call_user_func($callback, $this);
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Prototype;
namespace DesignPatterns\Creational\Prototype;
/**
* Class BarBookPrototype

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Prototype;
namespace DesignPatterns\Creational\Prototype;
/**
* class BookPrototype

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Prototype;
namespace DesignPatterns\Creational\Prototype;
/**
* Class FooBookPrototype

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Prototype;
namespace DesignPatterns\Creational\Prototype;
$fooPrototype = new FooBookPrototype();
$barPrototype = new BarBookPrototype();

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\SimpleFactory;
namespace DesignPatterns\Creational\SimpleFactory;
/**
* Bicycle is a bicycle

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\SimpleFactory;
namespace DesignPatterns\Creational\SimpleFactory;
/**
* class ConcreteFactory

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\SimpleFactory;
namespace DesignPatterns\Creational\SimpleFactory;
/**
* Scooter is a Scooter

View File

@@ -1,8 +1,8 @@
<?php
namespace DesignPatterns\Tests\SimpleFactory;
namespace DesignPatterns\Creational\SimpleFactory;
use DesignPatterns\SimpleFactory\ConcreteFactory;
use DesignPatterns\Creational\SimpleFactory\ConcreteFactory;
/**
* SimpleFactoryTest tests the Simple Factory pattern
@@ -31,7 +31,7 @@ class SimpleFactoryTest extends \PHPUnit_Framework_TestCase
public function testCreation($type)
{
$obj = $this->factory->createVehicle($type);
$this->assertInstanceOf('DesignPatterns\SimpleFactory\VehicleInterface', $obj);
$this->assertInstanceOf('DesignPatterns\Creational\SimpleFactory\VehicleInterface', $obj);
}
/**

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\SimpleFactory;
namespace DesignPatterns\Creational\SimpleFactory;
/**
* VehicleInterface is a contract for a vehicle

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\Singleton;
namespace DesignPatterns\Creational\Singleton;
/**
* class Singleton
@@ -8,7 +8,7 @@ namespace DesignPatterns\Singleton;
class Singleton
{
/**
* @var cached reference to singleton instance
* @var Singleton reference to singleton instance
*/
private static $instance;

View File

@@ -1,8 +1,8 @@
<?php
namespace DesignPatterns\Tests\Singleton;
namespace DesignPatterns\Creational\Singleton;
use DesignPatterns\Singleton\Singleton;
use DesignPatterns\Creational\Singleton\Singleton;
/**
* SingletonTest tests the singleton pattern
@@ -13,7 +13,7 @@ class SingletonTest extends \PHPUnit_Framework_TestCase
public function testUniqueness()
{
$firstCall = Singleton::getInstance();
$this->assertInstanceOf('DesignPatterns\Singleton\Singleton', $firstCall);
$this->assertInstanceOf('DesignPatterns\Creational\Singleton\Singleton', $firstCall);
$secondCall = Singleton::getInstance();
$this->assertSame($firstCall, $secondCall);
}

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\StaticFactory;
namespace DesignPatterns\Creational\StaticFactory;
/**
* Class FormatNumber

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\StaticFactory;
namespace DesignPatterns\Creational\StaticFactory;
/**
* Class FormatString

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\StaticFactory;
namespace DesignPatterns\Creational\StaticFactory;
/**
* Class FormatterInterface

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns\StaticFactory;
namespace DesignPatterns\Creational\StaticFactory;
/**
* Note1: Remember, static => global => evil

View File

@@ -1,8 +1,8 @@
<?php
namespace DesignPatterns\Tests\StaticFactory;
namespace DesignPatterns\Creational\StaticFactory;
use DesignPatterns\StaticFactory\StaticFactory;
use DesignPatterns\Creational\StaticFactory\StaticFactory;
/**
* Tests for Static Factory pattern
@@ -25,6 +25,6 @@ class StaticFactoryTest extends \PHPUnit_Framework_TestCase
public function testCreation($type)
{
$obj = StaticFactory::factory($type);
$this->assertInstanceOf('DesignPatterns\StaticFactory\FormatterInterface', $obj);
$this->assertInstanceOf('DesignPatterns\Creational\StaticFactory\FormatterInterface', $obj);
}
}