Analysis on the benefits of using php factory pattern

黄舟
Release: 2023-03-17 12:22:01
Original
1578 people have browsed it

We have introduced to you the three types of phpfactory mode before, as well as what is the php factory mode and why we should use the php factory mode. So today we will give you a detailed introduction to the php factory. Benefits of using patterns!

Generally when we instantiate a class, we will give it some parameters so that when it is constructed, we can feedback the results we need based on different parameters.
For example, the following is a User class, very simple:

The code is as follows:

<?php
interface IUser{
   function getName();
   function getAge();
}
class User implements IUser{
   protected $_name;
   protected $_age;
   function construct($name, $age){
      $this->_name = $name;
      $this->_age = (int)$age;
   }
   function getName(){
      return $this->_name;
   }
  function getAge(){
      return $this->_age;
   }
}
?>
Copy after login

If we want an instance This is how to transform this class:
$u = new User('Xiao Ming',19);
Generally, if this class is rarely used, then there is nothing wrong with this. Great impact, very good too.
Suddenly I want to add a classification to this class and put Xiao Ming into the student group. It is very easy to modify the code of the next class, but if this class is instantiated many times in many files before we want to modify it, then If you want to add a parameter to it, it will become very cumbersome, because you need to replace it with:
$u = new User('Xiaoming',19,'student');
Of course we can also avoid this duplication of work by setting default values ​​in the construct function, but in fact this is not good from the perspective of code elegance. Imagine we have a factory method that can By using an identifier to correspond to a set of parameters, and storing the parameters in a text document or directly in the factory class in the form of array, it will become easier when we call the User class. Many, even if you need to increase or decrease parametersAttributes, you don’t need to replace the code everywhere. The following is a factory class (you can also store the method directly in the User class)

Code As follows:

interface IUser{
    function getName();
    function getAge();
}
class User implements IUser{
   protected $_group;
   protected $_name;
   protected $_age;
  function construct($name, $age, $group){
      $this->_group = $group;
      $this->_name = $name;
      $this->_age = (int)$age;
  }
  function getName(){
     return $this->_name;
  }
  function getAge(){
      return $this->_age;
  }
}
class Fuser{
   private static $group = array(
       array(‘小明‘,19,‘学生‘),
       array(‘小王‘,19,‘学生‘)
   );
   static function create($id){
      list($name, $age, $group) = self::$group[(int)$id];
      return new User($name, $age, $group);
   }
}
echo Fuser::create(0)->getName();
Copy after login

The result should be the output "Xiao Ming".

Summary:

I believe that everyone has a certain understanding of the benefits of using the PHP factory mode. Put what you learn into practice in your own work!

Related recommendations:

A brief introduction to the three PHP factory modes


#What is php factory pattern? Why use php factory mode?


Detailed explanation of the three forms of sample code of PHP factory mode

The above is the detailed content of Analysis on the benefits of using php factory pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template