From d4ae3bcd8f99f53670b5e945ab2ea633ce5ed386 Mon Sep 17 00:00:00 2001 From: Trismegiste Date: Fri, 10 May 2013 21:29:43 +0200 Subject: [PATCH] use constant instead of string --- FactoryMethod/FactoryMethod.php | 17 +++++++++++++---- FactoryMethod/GermanFactory.php | 13 ++++++++++--- FactoryMethod/ItalianFactory.php | 7 +++++-- FactoryMethod/Porsche.php | 5 +++++ Tests/FactoryMethod/FactoryMethodTest.php | 17 +++++++++++------ 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/FactoryMethod/FactoryMethod.php b/FactoryMethod/FactoryMethod.php index 439965b..3402bd6 100644 --- a/FactoryMethod/FactoryMethod.php +++ b/FactoryMethod/FactoryMethod.php @@ -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 */ diff --git a/FactoryMethod/GermanFactory.php b/FactoryMethod/GermanFactory.php index a7fb6f1..a962c76 100644 --- a/FactoryMethod/GermanFactory.php +++ b/FactoryMethod/GermanFactory.php @@ -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"); } diff --git a/FactoryMethod/ItalianFactory.php b/FactoryMethod/ItalianFactory.php index 873c91d..db8514c 100644 --- a/FactoryMethod/ItalianFactory.php +++ b/FactoryMethod/ItalianFactory.php @@ -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"); } diff --git a/FactoryMethod/Porsche.php b/FactoryMethod/Porsche.php index 8ecf3ec..c2e775b 100644 --- a/FactoryMethod/Porsche.php +++ b/FactoryMethod/Porsche.php @@ -17,4 +17,9 @@ class Porsche implements Vehicle } + public function addTuningAMG() + { + + } + } \ No newline at end of file diff --git a/Tests/FactoryMethod/FactoryMethodTest.php b/Tests/FactoryMethod/FactoryMethodTest.php index ec7b652..463c1c8 100644 --- a/Tests/FactoryMethod/FactoryMethodTest.php +++ b/Tests/FactoryMethod/FactoryMethodTest.php @@ -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'); }