可调用工厂有任何缺点吗?例如:
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');
我错过了什么吗?谢谢!