start a restructure

This commit is contained in:
Antonio Spinelli
2014-03-21 18:03:44 -03:00
parent b0b0d4a1a4
commit e59d70a0ac
180 changed files with 21 additions and 16 deletions

View File

@@ -0,0 +1,19 @@
<?php
namespace DesignPatterns\SimpleFactory;
/**
* Bicycle is a bicycle
*/
class Bicycle implements VehicleInterface
{
/**
* @param mixed $destination
*
* @return mixed|void
*/
public function driveTo($destination)
{
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace DesignPatterns\SimpleFactory;
/**
* class ConcreteFactory
*/
class ConcreteFactory
{
/**
* @var array
*/
protected $typeList;
/**
* You can imagine to inject your own type list or merge with
* the default ones...
*/
public function __construct()
{
$this->typeList = array(
'bicycle' => __NAMESPACE__ . '\Bicycle',
'other' => __NAMESPACE__ . '\Scooter'
);
}
/**
* Creates a vehicle
*
* @param string $type a known type key
*
* @return VehicleInterface a new instance of VehicleInterface
* @throws \InvalidArgumentException
*/
public function createVehicle($type)
{
if (!array_key_exists($type, $this->typeList)) {
throw new \InvalidArgumentException("$type is not valid vehicle");
}
$className = $this->typeList[$type];
return new $className();
}
}

View File

@@ -0,0 +1,9 @@
# Simple Factory
## Purpose
ConcreteFactory is a simple factory pattern.
It differs from the static factory because it is NOT static and as you know: static => global => evil!
Therefore, you can haZ multiple factories, differently parametrized, you can subclass it and you can mock-up it.

View File

@@ -0,0 +1,17 @@
<?php
namespace DesignPatterns\SimpleFactory;
/**
* Scooter is a Scooter
*/
class Scooter implements VehicleInterface
{
/**
* @param mixed $destination
*/
public function driveTo($destination)
{
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace DesignPatterns\Tests\SimpleFactory;
use DesignPatterns\SimpleFactory\ConcreteFactory;
/**
* SimpleFactoryTest tests the Simple Factory pattern
*/
class SimpleFactoryTest extends \PHPUnit_Framework_TestCase
{
protected $factory;
protected function setUp()
{
$this->factory = new ConcreteFactory();
}
public function getType()
{
return array(
array('bicycle'),
array('other')
);
}
/**
* @dataProvider getType
*/
public function testCreation($type)
{
$obj = $this->factory->createVehicle($type);
$this->assertInstanceOf('DesignPatterns\SimpleFactory\VehicleInterface', $obj);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testBadType()
{
$this->factory->createVehicle('car');
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace DesignPatterns\SimpleFactory;
/**
* VehicleInterface is a contract for a vehicle
*/
interface VehicleInterface
{
/**
* @param mixed $destination
*
* @return mixed
*/
public function driveTo($destination);
}