我试图理解为什么应该使用工厂而不是 new
运算符来创建对象?例如:
$validatePost = Validation::factory($_POST);
代替
$validatePost = new Validation($_POST);
类的静态方法factory
做的完全一样:
public static function factory(array $array)
{
return new Validation($array);
}
最佳答案
根据以下问题:
Constructors vs Factory Methods
什么时候使用工厂方法模式
a class can't anticipate the class of objects it must create
a class wants its subclasses to specify the objects it creates
classes delegate responsibility to one of several helper subclasses, and you want to
localize the knowledge of which helper subclass is the delegate
关于php - 工厂的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19218149/