PHP7 SimpleFactory

This commit is contained in:
Dominik Liebler
2016-09-22 15:05:25 +02:00
parent f4c9af503b
commit 370ce4dd93
9 changed files with 275 additions and 443 deletions

View File

@ -2,17 +2,9 @@
namespace DesignPatterns\Creational\SimpleFactory;
/**
* Bicycle is a bicycle.
*/
class Bicycle implements VehicleInterface
class Bicycle
{
/**
* @param mixed $destination
*
* @return mixed|void
*/
public function driveTo($destination)
public function driveTo(string $destination)
{
}
}

View File

@ -6,11 +6,9 @@ Purpose
SimpleFactory 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 have multiple factories, differently parametrized,
you can subclass it and you can mock-up it.
It differs from the static factory because it is not static.
Therefore, you can have multiple factories, differently parametrized, you can subclass it and you can mock it.
It always should be preferred over a static factory!
UML Diagram
-----------
@ -42,12 +40,6 @@ Bicycle.php
:language: php
:linenos:
Scooter.php
.. literalinclude:: Scooter.php
:language: php
:linenos:
Usage
-----

View File

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

View File

@ -2,44 +2,10 @@
namespace DesignPatterns\Creational\SimpleFactory;
/**
* class SimpleFactory.
*/
class SimpleFactory
{
/**
* @var array
*/
protected $typeList;
/**
* You can imagine to inject your own type list or merge with
* the default ones...
*/
public function __construct()
public function createBicycle(): Bicycle
{
$this->typeList = array(
'bicycle' => __NAMESPACE__.'\Bicycle',
'other' => __NAMESPACE__.'\Scooter',
);
}
/**
* Creates a vehicle.
*
* @param string $type a known type key
*
* @throws \InvalidArgumentException
*
* @return VehicleInterface a new instance of VehicleInterface
*/
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();
return new Bicycle();
}
}

View File

@ -4,40 +4,11 @@ namespace DesignPatterns\Creational\SimpleFactory\Tests;
use DesignPatterns\Creational\SimpleFactory\SimpleFactory;
/**
* SimpleFactoryTest tests the Simple Factory pattern.
*/
class SimpleFactoryTest extends \PHPUnit_Framework_TestCase
{
protected $factory;
protected function setUp()
public function testCanCreateBicycle()
{
$this->factory = new SimpleFactory();
}
public function getType()
{
return array(
array('bicycle'),
array('other'),
);
}
/**
* @dataProvider getType
*/
public function testCreation($type)
{
$obj = $this->factory->createVehicle($type);
$this->assertInstanceOf('DesignPatterns\Creational\SimpleFactory\VehicleInterface', $obj);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testBadType()
{
$this->factory->createVehicle('car');
$bicycle = (new SimpleFactory())->createBicycle();
$this->assertInstanceOf('DesignPatterns\Creational\SimpleFactory\Bicycle', $bicycle);
}
}

View File

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

View File

@ -1,36 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<Diagram>
<ID>PHP</ID>
<OriginalElement>\DesignPatterns\Creational\SimpleFactory\SimpleFactory</OriginalElement>
<nodes>
<node x="173.0" y="97.0">\DesignPatterns\Creational\SimpleFactory\Scooter</node>
<node x="0.0" y="189.0">\DesignPatterns\Creational\SimpleFactory\SimpleFactory</node>
<node x="86.5" y="0.0">\DesignPatterns\Creational\SimpleFactory\VehicleInterface</node>
<node x="0.0" y="97.0">\DesignPatterns\Creational\SimpleFactory\Bicycle</node>
</nodes>
<notes />
<edges>
<edge source="\DesignPatterns\Creational\SimpleFactory\Bicycle" target="\DesignPatterns\Creational\SimpleFactory\VehicleInterface">
<point x="0.0" y="-23.5" />
<point x="76.5" y="72.0" />
<point x="124.75" y="72.0" />
<point x="-38.25" y="23.5" />
</edge>
<edge source="\DesignPatterns\Creational\SimpleFactory\Scooter" target="\DesignPatterns\Creational\SimpleFactory\VehicleInterface">
<point x="0.0" y="-23.5" />
<point x="249.5" y="72.0" />
<point x="201.25" y="72.0" />
<point x="38.25" y="23.5" />
</edge>
</edges>
<settings layout="Hierarchic Group" zoom="1.0" x="163.0" y="139.0" />
<SelectedNodes />
<Categories>
<Category>Fields</Category>
<Category>Constants</Category>
<Category>Constructors</Category>
<Category>Methods</Category>
</Categories>
<VISIBILITY>private</VISIBILITY>
</Diagram>
<?xml version="1.0" encoding="UTF-8"?>
<Diagram>
<ID>PHP</ID>
<OriginalElement>\DesignPatterns\Creational\SimpleFactory\Bicycle</OriginalElement>
<nodes>
<node x="0.0" y="0.0">\DesignPatterns\Creational\SimpleFactory\Bicycle</node>
<node x="0.0" y="96.0">\DesignPatterns\Creational\SimpleFactory\SimpleFactory</node>
</nodes>
<notes />
<edges />
<settings layout="Hierarchic Group" zoom="1.0" x="60.0" y="51.5" />
<SelectedNodes />
<Categories>
<Category>Methods</Category>
<Category>Constants</Category>
<Category>Fields</Category>
</Categories>
<VISIBILITY>private</VISIBILITY>
</Diagram>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 17 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 39 KiB