mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-22 06:21:46 +02:00
cs FactoryMethod
This commit is contained in:
parent
754ea98fb2
commit
b05f57064f
@ -1,9 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\FactoryMethod;
|
namespace DesignPatterns\FactoryMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,10 +7,18 @@ namespace DesignPatterns\FactoryMethod;
|
|||||||
*/
|
*/
|
||||||
class Bicycle implements Vehicle
|
class Bicycle implements Vehicle
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $color;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets the color of the bicycle
|
||||||
|
*
|
||||||
|
* @param string $rgb
|
||||||
|
*/
|
||||||
public function setColor($rgb)
|
public function setColor($rgb)
|
||||||
{
|
{
|
||||||
|
$this->color = $rgb;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,9 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\FactoryMethod;
|
namespace DesignPatterns\FactoryMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,5 +46,4 @@ abstract class FactoryMethod
|
|||||||
|
|
||||||
return $obj;
|
return $obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,9 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\FactoryMethod;
|
namespace DesignPatterns\FactoryMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,10 +7,16 @@ namespace DesignPatterns\FactoryMethod;
|
|||||||
*/
|
*/
|
||||||
class Ferrari implements Vehicle
|
class Ferrari implements Vehicle
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $color;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $rgb
|
||||||
|
*/
|
||||||
public function setColor($rgb)
|
public function setColor($rgb)
|
||||||
{
|
{
|
||||||
|
$this->color = $rgb;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,39 +1,31 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\FactoryMethod;
|
namespace DesignPatterns\FactoryMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GermanFactory is vehicle factory in Germany
|
* GermanFactory is a vehicle factory in Germany
|
||||||
*/
|
*/
|
||||||
class GermanFactory extends FactoryMethod
|
class GermanFactory extends FactoryMethod
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
protected function createVehicle($type)
|
protected function createVehicle($type)
|
||||||
{
|
{
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
|
case parent::CHEAP:
|
||||||
case parent::CHEAP :
|
|
||||||
return new Bicycle();
|
return new Bicycle();
|
||||||
break;
|
break;
|
||||||
|
case parent::FAST:
|
||||||
case parent::FAST :
|
|
||||||
$obj = new Porsche();
|
$obj = new Porsche();
|
||||||
// we can specialize the way we want some concrete Vehicle since
|
// we can specialize the way we want some concrete Vehicle since
|
||||||
// we know the class
|
// we know the class
|
||||||
$obj->addTuningAMG();
|
$obj->addTuningAMG();
|
||||||
|
|
||||||
return $obj;
|
return $obj;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default :
|
default :
|
||||||
throw new \InvalidArgumentException("$type is not a valid vehicle");
|
throw new \InvalidArgumentException("$type is not a valid vehicle");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,9 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\FactoryMethod;
|
namespace DesignPatterns\FactoryMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,25 +7,20 @@ namespace DesignPatterns\FactoryMethod;
|
|||||||
*/
|
*/
|
||||||
class ItalianFactory extends FactoryMethod
|
class ItalianFactory extends FactoryMethod
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
protected function createVehicle($type)
|
protected function createVehicle($type)
|
||||||
{
|
{
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
|
case parent::CHEAP:
|
||||||
case parent::CHEAP :
|
|
||||||
return new Bicycle();
|
return new Bicycle();
|
||||||
break;
|
break;
|
||||||
|
case parent::FAST:
|
||||||
case parent::FAST :
|
|
||||||
return new Ferrari();
|
return new Ferrari();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default :
|
default :
|
||||||
throw new \InvalidArgumentException("$type is not a valid vehicle");
|
throw new \InvalidArgumentException("$type is not a valid vehicle");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,9 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\FactoryMethod;
|
namespace DesignPatterns\FactoryMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,15 +7,25 @@ namespace DesignPatterns\FactoryMethod;
|
|||||||
*/
|
*/
|
||||||
class Porsche implements Vehicle
|
class Porsche implements Vehicle
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $color;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $rgb
|
||||||
|
*/
|
||||||
public function setColor($rgb)
|
public function setColor($rgb)
|
||||||
{
|
{
|
||||||
|
$this->color = $rgb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* although tuning by AMG is only offered for Mercedes Cars,
|
||||||
|
* this is a valid coding example ...
|
||||||
|
*/
|
||||||
public function addTuningAMG()
|
public function addTuningAMG()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,9 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\FactoryMethod;
|
namespace DesignPatterns\FactoryMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,6 +7,10 @@ namespace DesignPatterns\FactoryMethod;
|
|||||||
*/
|
*/
|
||||||
interface Vehicle
|
interface Vehicle
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* sets the color of the vehicle
|
||||||
|
*
|
||||||
|
* @param string $rgb
|
||||||
|
*/
|
||||||
function setColor($rgb);
|
function setColor($rgb);
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user