可调用工厂有任何缺点吗?

时间:2015-10-22 15:29:58

标签: php oop design-patterns factory

可调用工厂有任何缺点吗?例如:

class EntityFactory
{
    protected $map = [
        'User' => User::class,
    ];        

    public function __invoke(string $entity, ...$args)
    {
        if (! $class = $this->map[$entity] ?? null) {
            throw new InvalidArgumentException(sprintf('Unknown entity "%s"', $entity));
        }

        return new $class(...$args);
    }
}

后来:

$entityFactory = new EntityFactory;
$john = $entityFactory('User', 'John');
$jane = $entityFactory('User', 'Jane');

我错过了什么吗?谢谢!

0 个答案:

没有答案