use constant instead of string

This commit is contained in:
Trismegiste
2013-05-10 21:29:43 +02:00
parent cc765bde41
commit d4ae3bcd8f
5 changed files with 44 additions and 15 deletions

View File

@@ -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
*/

View File

@@ -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");
}

View File

@@ -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");
}

View File

@@ -17,4 +17,9 @@ class Porsche implements Vehicle
}
public function addTuningAMG()
{
}
}