mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-03 21:47:25 +02:00
PHP7 SimpleFactory
This commit is contained in:
@@ -2,17 +2,9 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\SimpleFactory;
|
namespace DesignPatterns\Creational\SimpleFactory;
|
||||||
|
|
||||||
/**
|
class Bicycle
|
||||||
* Bicycle is a bicycle.
|
|
||||||
*/
|
|
||||||
class Bicycle implements VehicleInterface
|
|
||||||
{
|
{
|
||||||
/**
|
public function driveTo(string $destination)
|
||||||
* @param mixed $destination
|
|
||||||
*
|
|
||||||
* @return mixed|void
|
|
||||||
*/
|
|
||||||
public function driveTo($destination)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,11 +6,9 @@ Purpose
|
|||||||
|
|
||||||
SimpleFactory is a simple factory pattern.
|
SimpleFactory is a simple factory pattern.
|
||||||
|
|
||||||
It differs from the static factory because it is NOT static and as you
|
It differs from the static factory because it is not static.
|
||||||
know: static => global => evil!
|
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!
|
||||||
Therefore, you can have multiple factories, differently parametrized,
|
|
||||||
you can subclass it and you can mock-up it.
|
|
||||||
|
|
||||||
UML Diagram
|
UML Diagram
|
||||||
-----------
|
-----------
|
||||||
@@ -42,12 +40,6 @@ Bicycle.php
|
|||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
Scooter.php
|
|
||||||
|
|
||||||
.. literalinclude:: Scooter.php
|
|
||||||
:language: php
|
|
||||||
:linenos:
|
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
@@ -1,16 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Creational\SimpleFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Scooter is a Scooter.
|
|
||||||
*/
|
|
||||||
class Scooter implements VehicleInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param mixed $destination
|
|
||||||
*/
|
|
||||||
public function driveTo($destination)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
@@ -2,44 +2,10 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\SimpleFactory;
|
namespace DesignPatterns\Creational\SimpleFactory;
|
||||||
|
|
||||||
/**
|
|
||||||
* class SimpleFactory.
|
|
||||||
*/
|
|
||||||
class SimpleFactory
|
class SimpleFactory
|
||||||
{
|
{
|
||||||
/**
|
public function createBicycle(): Bicycle
|
||||||
* @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(
|
return new Bicycle();
|
||||||
'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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,40 +4,11 @@ namespace DesignPatterns\Creational\SimpleFactory\Tests;
|
|||||||
|
|
||||||
use DesignPatterns\Creational\SimpleFactory\SimpleFactory;
|
use DesignPatterns\Creational\SimpleFactory\SimpleFactory;
|
||||||
|
|
||||||
/**
|
|
||||||
* SimpleFactoryTest tests the Simple Factory pattern.
|
|
||||||
*/
|
|
||||||
class SimpleFactoryTest extends \PHPUnit_Framework_TestCase
|
class SimpleFactoryTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
protected $factory;
|
public function testCanCreateBicycle()
|
||||||
|
|
||||||
protected function setUp()
|
|
||||||
{
|
{
|
||||||
$this->factory = new SimpleFactory();
|
$bicycle = (new SimpleFactory())->createBicycle();
|
||||||
}
|
$this->assertInstanceOf('DesignPatterns\Creational\SimpleFactory\Bicycle', $bicycle);
|
||||||
|
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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);
|
|
||||||
}
|
|
@@ -1,36 +1,20 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<Diagram>
|
<Diagram>
|
||||||
<ID>PHP</ID>
|
<ID>PHP</ID>
|
||||||
<OriginalElement>\DesignPatterns\Creational\SimpleFactory\SimpleFactory</OriginalElement>
|
<OriginalElement>\DesignPatterns\Creational\SimpleFactory\Bicycle</OriginalElement>
|
||||||
<nodes>
|
<nodes>
|
||||||
<node x="173.0" y="97.0">\DesignPatterns\Creational\SimpleFactory\Scooter</node>
|
<node x="0.0" y="0.0">\DesignPatterns\Creational\SimpleFactory\Bicycle</node>
|
||||||
<node x="0.0" y="189.0">\DesignPatterns\Creational\SimpleFactory\SimpleFactory</node>
|
<node x="0.0" y="96.0">\DesignPatterns\Creational\SimpleFactory\SimpleFactory</node>
|
||||||
<node x="86.5" y="0.0">\DesignPatterns\Creational\SimpleFactory\VehicleInterface</node>
|
</nodes>
|
||||||
<node x="0.0" y="97.0">\DesignPatterns\Creational\SimpleFactory\Bicycle</node>
|
<notes />
|
||||||
</nodes>
|
<edges />
|
||||||
<notes />
|
<settings layout="Hierarchic Group" zoom="1.0" x="60.0" y="51.5" />
|
||||||
<edges>
|
<SelectedNodes />
|
||||||
<edge source="\DesignPatterns\Creational\SimpleFactory\Bicycle" target="\DesignPatterns\Creational\SimpleFactory\VehicleInterface">
|
<Categories>
|
||||||
<point x="0.0" y="-23.5" />
|
<Category>Methods</Category>
|
||||||
<point x="76.5" y="72.0" />
|
<Category>Constants</Category>
|
||||||
<point x="124.75" y="72.0" />
|
<Category>Fields</Category>
|
||||||
<point x="-38.25" y="23.5" />
|
</Categories>
|
||||||
</edge>
|
<VISIBILITY>private</VISIBILITY>
|
||||||
<edge source="\DesignPatterns\Creational\SimpleFactory\Scooter" target="\DesignPatterns\Creational\SimpleFactory\VehicleInterface">
|
</Diagram>
|
||||||
<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>
|
|
||||||
|
|
||||||
|
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 |
Reference in New Issue
Block a user