使用工厂模式用于各种组件/模块

时间:2016-11-07 19:54:06

标签: oop design-patterns factory-pattern solid-principles

我们说我们有一个制作文章的系统。这篇文章有一些组件验证器,清理器,存储....在客户端构建一篇文章我必须实例化每个组件:

    $title = 'title';
    $description = 'Description in html';

    //Cleaner just clean some things from each field.
    $cleaner = new Cleaner();

    //Validator throw exception if something is not correct
    $validator = new Validator();

    // Storage save files and article itself
    $storage = new Storage();

    //Dom Class get some files from description field
    $dom = new Dom();
    $files = $dom->getFiles($description);

    $storage->files($files);

    $article = new ArticleBuilder();
    $article->addTitle($validator->title($cleaner->title($title)));
    $article->addDescription($validator->description($cleaner->description($description)));
    $article->add....

如果没有这些,就无法建立一篇文章。

我的问题是: 我可以使用工厂模式创建所有这些:

          class ArticleFactory
        {
            private $article;

            public function __construct()
            {
                $this->article = new ArticleBuilder();
            }

            public function setTitle(string $title)
            {
                $title = ($this->validator())->title($title);
                $title = ($this->cleaner())->title($title);
                $this->article->addTitle($title);
            }

            public function setDescription(string $des)
            {
                $des = ($this->validator())->title($des);
                $des = ($this->cleaner())->title($des);
                $this->article->addDescription($des);
            }

            public function getArticle(): ArticleBuilder
            {
                return $this->article;
            }

            public function getFiles($description)
            {
               return ($this->dom())->getFiles($description);
            }

            public function storeFile($files)
            {
                ($this->storage())->files($files);
            }

            public function validator(): ValidatorInterface
            {
                return new Validator();
            }

            public function cleaner(): CleanerInterface
            {
                return new Cleaner();
            }

            public function storage(): StorageInterface
            {
                return new Storage();
            }

            public function dom(): DomInterface
            {
                return new Dom();
            }
        }

在客户端使用上述工厂创建文章更方便:

        $myTitle = 'my title';
        $myDes = 'mty description';
        $article = new ArticleFactory();
        $article->setTitle($myTitle);
        $article->setDescription($myDes);
        $files = $article->getFiles($description);
        $article->storeFile($files);

这违反了任何SOLID原则吗?

有没有更好的方法呢?

1 个答案:

答案 0 :(得分:-1)

班级ArticleFactory似乎违反了SRP,因为ArticleFactory涉及不止一件事(构建,存储,验证和清理文章)。

Factory模式和Builder模式之间似乎也存在混淆。如果类ArticleFactory也构建了文章,那么如果它具有(组合)构建器并将构建过程委托给它,那么它将更加清晰。你真的需要一个建设者吗?创建新文章的过程是如此复杂/昂贵,以至于构建器模式会增加价值吗?

在函数名称(function ValidatorCleanerStorage)中使用名词会使代码难以理解。你的意图是什么?

使用动词作为类的函数和名词。