1
0
mirror of https://github.com/DesignPatternsPHP/DesignPatternsPHP.git synced 2025-07-23 08:11:17 +02:00

the REAL factory method pattern

This commit is contained in:
Trismegiste
2013-05-10 21:09:55 +02:00
parent 9b7330795d
commit cc765bde41
8 changed files with 235 additions and 0 deletions

@@ -0,0 +1,45 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\FactoryMethod;
/**
* FactoryMethod is a factory method. The good point on the SimpleFactory
* is you can subclass it to implement different way to create vehicle for
* each country.
*
* For simple case, this abstracct class could be just an interface
*/
abstract class FactoryMethod
{
/**
* 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 Vehicle a new vehicle
*/
abstract protected function createVehicle($type);
/**
* Creates a new vehicle
*
* @param string $type
*
* @return Vehicle a new vehicle
*/
public function create($type)
{
$obj = $this->createVehicle($type);
$obj->setColor("#f00");
return $obj;
}
}