mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-01 12:40:11 +02:00
use constant instead of string
This commit is contained in:
@@ -7,15 +7,24 @@
|
||||
namespace DesignPatterns\FactoryMethod;
|
||||
|
||||
/**
|
||||
* FactoryMethod is a factory method. The good point on the SimpleFactory
|
||||
* FactoryMethod is a factory method. The good point over the SimpleFactory
|
||||
* is you can subclass it to implement different way to create vehicle for
|
||||
* each country.
|
||||
* each country (see subclasses)
|
||||
*
|
||||
* For simple case, this abstracct class could be just an interface
|
||||
* For simple case, this abstract class could be just an interface
|
||||
*
|
||||
* This pattern is a "real" Design Pattern because it achieves the
|
||||
* "Dependency Inversion Principle" a.k.a the "D" in S.O.L.I.D principles.
|
||||
*
|
||||
* It means the FactoryMethod class depends on abstractions not concrete classes.
|
||||
* This is the real trick compared to SImpleFactory or StaticFactory.
|
||||
*/
|
||||
abstract class FactoryMethod
|
||||
{
|
||||
|
||||
const CHEAP = 1;
|
||||
const FAST = 2;
|
||||
|
||||
/**
|
||||
* The children of the class must implement this method
|
||||
*
|
||||
@@ -30,7 +39,7 @@ abstract class FactoryMethod
|
||||
/**
|
||||
* Creates a new vehicle
|
||||
*
|
||||
* @param string $type
|
||||
* @param int $type
|
||||
*
|
||||
* @return Vehicle a new vehicle
|
||||
*/
|
||||
|
@@ -18,12 +18,19 @@ class GermanFactory extends FactoryMethod
|
||||
protected function createVehicle($type)
|
||||
{
|
||||
switch ($type) {
|
||||
case 'cheap' :
|
||||
|
||||
case parent::CHEAP :
|
||||
return new Bicycle();
|
||||
break;
|
||||
case 'fast' :
|
||||
return new Porsche();
|
||||
|
||||
case parent::FAST :
|
||||
$obj = new Porsche();
|
||||
// we can specialize the way we want some concrete Vehicle since
|
||||
// we know the class
|
||||
$obj->addTuningAMG();
|
||||
return $obj;
|
||||
break;
|
||||
|
||||
default :
|
||||
throw new \InvalidArgumentException("$type is not a valid vehicle");
|
||||
}
|
||||
|
@@ -18,12 +18,15 @@ class ItalianFactory extends FactoryMethod
|
||||
protected function createVehicle($type)
|
||||
{
|
||||
switch ($type) {
|
||||
case 'cheap' :
|
||||
|
||||
case parent::CHEAP :
|
||||
return new Bicycle();
|
||||
break;
|
||||
case 'fast' :
|
||||
|
||||
case parent::FAST :
|
||||
return new Ferrari();
|
||||
break;
|
||||
|
||||
default :
|
||||
throw new \InvalidArgumentException("$type is not a valid vehicle");
|
||||
}
|
||||
|
@@ -17,4 +17,9 @@ class Porsche implements Vehicle
|
||||
|
||||
}
|
||||
|
||||
public function addTuningAMG()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -6,7 +6,9 @@
|
||||
|
||||
namespace DesignPatterns\Tests\FactoryMethod;
|
||||
|
||||
use DesignPatterns\FactoryMethod;
|
||||
use DesignPatterns\FactoryMethod\FactoryMethod;
|
||||
use DesignPatterns\FactoryMethod\GermanFactory;
|
||||
use DesignPatterns\FactoryMethod\ItalianFactory;
|
||||
|
||||
/**
|
||||
* FactoryMethodTest tests the factory method pattern
|
||||
@@ -14,20 +16,23 @@ use DesignPatterns\FactoryMethod;
|
||||
class FactoryMethodTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $type = array('cheap', 'fast');
|
||||
protected $type = array(
|
||||
FactoryMethod::CHEAP,
|
||||
FactoryMethod::FAST
|
||||
);
|
||||
|
||||
public function getShop()
|
||||
{
|
||||
return array(
|
||||
array(new FactoryMethod\GermanFactory()),
|
||||
array(new FactoryMethod\ItalianFactory())
|
||||
array(new GermanFactory()),
|
||||
array(new ItalianFactory())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getShop
|
||||
*/
|
||||
public function testCreation(FactoryMethod\FactoryMethod $shop)
|
||||
public function testCreation(FactoryMethod $shop)
|
||||
{
|
||||
// this test method acts as a client for the factory. We don't care
|
||||
// about the factory, all we know is it can produce vehicle
|
||||
@@ -42,7 +47,7 @@ class FactoryMethodTest extends \PHPUnit_Framework_TestCase
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage spaceship is not a valid vehicle
|
||||
*/
|
||||
public function testUnknownType(FactoryMethod\FactoryMethod $shop)
|
||||
public function testUnknownType(FactoryMethod $shop)
|
||||
{
|
||||
$shop->create('spaceship');
|
||||
}
|
||||
|
Reference in New Issue
Block a user