mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
start a restructure
This commit is contained in:
44
Creational/SimpleFactory/ConcreteFactory.php
Normal file
44
Creational/SimpleFactory/ConcreteFactory.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\SimpleFactory;
|
||||
|
||||
/**
|
||||
* class ConcreteFactory
|
||||
*/
|
||||
class ConcreteFactory
|
||||
{
|
||||
/**
|
||||
* @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(
|
||||
'bicycle' => __NAMESPACE__ . '\Bicycle',
|
||||
'other' => __NAMESPACE__ . '\Scooter'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a vehicle
|
||||
*
|
||||
* @param string $type a known type key
|
||||
*
|
||||
* @return VehicleInterface a new instance of VehicleInterface
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user