Home >Backend Development >PHP Tutorial >What is the role of the PHP design pattern factory pattern?

What is the role of the PHP design pattern factory pattern?

黄舟
黄舟Original
2017-11-13 15:03:593311browse

We have introduced you to the benefits of phpfactory mode, the meaning of factory mode, and the three situations of factory mode. So today we will continue to introduce to you the role of php factory mode!

Factory patterns are divided into three types: simple factory, factory method, and abstract factory. The difference between the three types of factories is that the abstract factory has multiple product lines, while the factory method has only one product line, which is a simplification of the abstract factory. . The factory method is opposite to the simple factory. At first glance, it seems that the factory method adds a lot of code but implements the same functions as the simple factory. But the essence is that the simple factory does not strictly follow the opening and closing principle of the design pattern. When new products need to be added, the factory code also needs to be modified. However, the factory method strictly adheres to the opening and closing principle. The mode is only responsible for the abstract factory interface, and the specific factory is left to the customer to expand. In the division of labor, core engineers are responsible for the definition of abstract factories and abstract products, and business engineers are responsible for the implementation of specific factories and specific products. As long as the abstraction layer is well designed, the framework is very stable.

Simple Factory

Advantages: Product users do not need to care about the creation process of product classes, and achieve decoupling effect from the implementation classes of specific products;

Disadvantages: Violation of " Open-Closed "principle (OCP), because when adding a new product class, the original factory method needs to be modified;

Applicable occasions: When the factory class is responsible for creating fewer objects when;

Implementation method: define a simple common class, and define a static production method, and generate the corresponding specific parent class based on the type parameter passed in

The specific code is as follows

<?php//抽象产品interface Person {
    public function getName(); }//具体产品实现class Teacher implements Person {
    public function getName() {
        return "老师n";
    }}class Student implements Person {
    public function getName() {
        return "学生n";
    }}//简单工厂class SimpleFactory {
    public static function getPerson($type) {
      $person = null;
      if ($type == &#39;teacher&#39;) {
        $person = new Teacher();
      } elseif ($type == &#39;student&#39;) {
        $person = new Student();
      }
      return $person;
    }}//简单工厂调用class SimpleClient {
    function main() {
      // 如果不用工厂模式,则需要提前指定具体类
      // $person = new Teacher();
      // echo $person->getName();
      // $person = new Student();
      // echo $person->getName();

      // 用工厂模式,则不需要知道对象由什么类产生,交给工厂去决定
      $person = SimpleFactory::getPerson(&#39;teacher&#39;);
      echo $person->getName();
      $person = SimpleFactory::getPerson(&#39;student&#39;);
      echo $person->getName();
    }}

Summary:

Through the study of this article, I believe that everyone has a new concept of the role of the PHP factory pattern. I hope it will be helpful to your work. ! ~

Related recommendations:

Analysis on the benefits of using PHP factory pattern


A brief introduction to the three PHP factory modes


What is the PHP factory mode? Why use php factory mode?

The above is the detailed content of What is the role of the PHP design pattern factory pattern?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn