/**
* 工厂模式
* 优点:
* 1.重命名或替换Car类,你只需要更改工厂类代码,而不是在每一个用到Car的地方更改。
* 2.创建对象过程复杂,也只需要在工厂类中写,而不必在每个创建实例的地方重复写。
* Class Car
*/
class Car
{
private $color;
private $type;
public function __construct($color, $type)
{
$this->color = $color;
$this->type = $type;
}
public function getColor()
{
return $this->color;
}
}
class CarFactory
{
public static function create($color, $type)
{
return new Car($color, $type);
}
}
$smallBus = CarFactory::create('black', 'small');
echo $smallBus->getColor();