PHP7 FactoryMethod

This commit is contained in:
Dominik Liebler
2016-09-22 13:28:46 +02:00
parent 6a98bcb73b
commit 4760d898a0
11 changed files with 94 additions and 159 deletions

View File

@@ -2,36 +2,17 @@
namespace DesignPatterns\Creational\FactoryMethod;
/**
* class FactoryMethod.
*/
abstract class FactoryMethod
{
const CHEAP = 1;
const FAST = 2;
const CHEAP = 'cheap';
const FAST = 'fast';
/**
* The children of the class must implement this method.
*
* Sometimes this method can be public to get "raw" object
*
* @param string $type a generic type
*
* @return VehicleInterface a new vehicle
*/
abstract protected function createVehicle($type);
abstract protected function createVehicle(string $type): VehicleInterface;
/**
* Creates a new vehicle.
*
* @param int $type
*
* @return VehicleInterface a new vehicle
*/
public function create($type)
public function create(string $type): VehicleInterface
{
$obj = $this->createVehicle($type);
$obj->setColor('#f00');
$obj->setColor('black');
return $obj;
}