mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-21 15:21:19 +02:00
(not design) pattern : simple factory
This commit is contained in:
20
SimpleFactory/Bicycle.php
Normal file
20
SimpleFactory/Bicycle.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* DesignPatternPHP
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace DesignPatterns\SimpleFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bicycle is a bicycle
|
||||||
|
*/
|
||||||
|
class Bicycle implements Vehicle
|
||||||
|
{
|
||||||
|
|
||||||
|
public function driveTo($destination)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
52
SimpleFactory/ConcreteFactory.php
Normal file
52
SimpleFactory/ConcreteFactory.php
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* DesignPatternPHP
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace DesignPatterns\SimpleFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
class ConcreteFactory
|
||||||
|
{
|
||||||
|
|
||||||
|
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 Vehicle a new instance of Vehicle
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
20
SimpleFactory/Scooter.php
Normal file
20
SimpleFactory/Scooter.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* DesignPatternPHP
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace DesignPatterns\SimpleFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scooter is a Scooter
|
||||||
|
*/
|
||||||
|
class Scooter implements Vehicle
|
||||||
|
{
|
||||||
|
|
||||||
|
public function driveTo($destination)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
16
SimpleFactory/Vehicle.php
Normal file
16
SimpleFactory/Vehicle.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* DesignPatternPHP
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace DesignPatterns\SimpleFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vehicle is a contract for a vehicle
|
||||||
|
*/
|
||||||
|
interface Vehicle
|
||||||
|
{
|
||||||
|
|
||||||
|
function driveTo($destination);
|
||||||
|
}
|
51
Tests/SimpleFactory/SimpleFactoryTest.php
Normal file
51
Tests/SimpleFactory/SimpleFactoryTest.php
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* DesignPatternPHP
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace DesignPatterns\Tests\SimpleFactory;
|
||||||
|
|
||||||
|
use DesignPatterns\SimpleFactory\ConcreteFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SimpleFactoryTest tests the Simple Factory pattern
|
||||||
|
*
|
||||||
|
* @author flo
|
||||||
|
*/
|
||||||
|
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\Vehicle', $obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testBadType()
|
||||||
|
{
|
||||||
|
$this->factory->createVehicle('car');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user