【发布时间】:2015-04-28 22:02:31
【问题描述】:
通常Factory 类包含类似getObject 的方法。
因此
class Factory
{
private $type;
function __construct($type)
{
$this->type = $type;
}
function getObject()
{
//example for brevity only to show use of $type variable
if ($this->type) $object = new $type();
return $object;
}
}
问题:为什么不直接通过构造函数返回对象?
class Factory
{
function __construct($type)
{
if ($type) $object = new $type();
return $object;
}
}
【问题讨论】:
-
请注意,有两种工厂模式,factory design pattern 和 factory-method pattern(略有不同)
-
@Dennis 我可以为你改进我的答案吗?
标签: php oop design-patterns factory factory-pattern