Home  >  Article  >  Backend Development  >  Detailed explanation of factory pattern of PHP design pattern

Detailed explanation of factory pattern of PHP design pattern

怪我咯
怪我咯Original
2017-07-12 14:07:111147browse

php design pattern summary-factory pattern

What is the purpose or goal of using the factory pattern?

The biggest advantage of the factory pattern is to create objects, which is to encapsulate the process of creating objects, so that a new object can be generated at any time.
Reduce the code and copy and paste, the coupling relationship is heavy, and it affects other parts of the code.

In layman's terms, in the past To create an object, you had to use new, but now this process is encapsulated.
Assuming that the factory mode is not used: then class a is called in many places, and the code will create an instance like this: new a(). Suppose that one day the name of class a needs to be modified, which means that many calling codes must be modified. .

The advantage of the factory pattern lies in creating objects.
The advantage of the factory pattern lies in creating objects. Establish a factory (a function or a class method) to create new objects. Its task is to encapsulate the object creation process.
Creating objects does not use new. Instead, define a method for creating object instances.

This article mainly introduces the factory mode and singleton mode of PHP design patterns, briefly introduces the functions of factory mode and singleton mode, and analyzes the implementation and implementation of factory mode and singleton mode in the form of examples. Application, friends in need can refer to it, is as follows:

Factory mode: Create corresponding objects in response to needs

class factory{
  function construct($name){
    if(file_exists('./'.$name.'.class.php')){
      return new $name;
    }else{
      die('not exist');
    }
  }
}

Single case mode: Only create an instance of the object , no more instances are allowed to be created, saving resources (such as database connections)

class instance{
  public $val = 10;
  private static $instance ;
  private function construct(){}
  private function clone(){}
  //设置为静态方法才可被类调用
  public static function getInstance(){
    /*if(!isset(self::$instance)){
      self::$instance = new self;
    }*/
    if(!isset(instance::$instance)){
      instance::$instance = new self;
    }
    return instance::$instance;
  }
}
$obj_one = instance::getInstance();
$obj_one->val = 20;
//clone可以调用clone()克隆即new出一个新的的对象
//$obj_two = clone $obj_one;
$obj_two = instance::getInstance();
echo $obj_two->val;
echo &#39;<p>&#39;;
var_dump($obj_one,$obj_two);

The running results are as follows:

20
object(instance)[1]
 public &#39;val&#39; => int 20
object(instance)[1]
 public &#39;val&#39; => int 20

Application: database access object

class mysqldb{
  private $arr = array(
    &#39;port&#39; => 3306,
    &#39;host&#39; => &#39;localhost&#39;,
    &#39;username&#39; => &#39;root&#39;,
    &#39;passward&#39; => &#39;root&#39;,
    &#39;dbname&#39; => &#39;instance&#39;,
    &#39;charset&#39; => &#39;utf8&#39;
     );
  private $link;
  static $instance;
  private function clone(){}
  private function construct(){
    $this->link = mysql_connect($this->arr[&#39;host&#39;],$this->arr[&#39;username&#39;],$this->arr[&#39;passward&#39;]) or die(mysql_error());
    mysql_select_db($this->arr[&#39;dbname&#39;]) or die(&#39;db error&#39;);
    mysql_set_charset($this->arr[&#39;charset&#39;]);
  }
  static public function getInsance(){
    if(!isset(mysqldb::$instance)){
      mysqldb::$instance = new self;
    }
    return mysqldb::$instance;
  }
  public function query($sql){
    if($res = mysql_query($sql)){
      return $res;
    }return false;
  }
  //fetch one
  public function get_one($sql){
    $res = $this->query($sql);
    if($result = mysql_fetch_row($res)){
      return $result[0];
    }
  }
  //fetch row
  public function get_row($sql){
    $res = $this->query($sql);
    if($result = mysql_fetch_assoc($res)){
      return $result;
    }
    return false;
  }
  //fetch all
  public function get_all($sql){
    $res = $this->query($sql);
    $arr = array();
    while($result = mysql_fetch_assoc($res)){
      $arr[] = $result;
    }
    return $arr;
  }
}
$mysql = mysqldb::getInsance();

The above is the detailed content of Detailed explanation of factory pattern of PHP design 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