php - 工厂的目的是什么?

标签 php kohana

我试图理解为什么应该使用工厂而不是 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/

相关文章:

sql - 如何在 Kohana 3 中配置 SQLite?

php - mustache php gettext()

php - Kohana 错误...尝试分配非对象的属性

php - 组合两个都实现 __() 国际化函数的 PHP 框架

javascript - 如何将 PHP 对象序列化为 JavaScript 对象(不是 JSON)

php - 从 PHP 中的文件处理程序释放内存

javascript - 限制 XML 文件中回显的 Ajax 回复数量

php - 无法在zend框架的 Controller 中加载模型

php - Doctrine 查询语法错误

php - 我如何通过跨域用户 session 加强这种安全性?