PHP Design Pattern Summary-Factory mode
Use the purpose or goal of the factory model?
The biggest advantage of the factory pattern is that the object is created by encapsulating the process of creating the object so that a new object can be generated at any time.
Reduce the code to copy the sticky, coupling the relationship heavy, lead the other part of the code.
In layman's terms, creating an object before uses new, and now encapsulates the process.
Suppose you do not use Factory mode: So many places call Class A, and the code creates an instance like this: New A (), assuming that one day you need to modify the name of Class A, meaning that many of the calling code is modified.
The advantage of the factory pattern is that it is created on the object.
The advantage of the factory pattern is that it is created on the object. Build a factory (a function or a class method) to create a new object whose task is to encapsulate the creation of the object,
The creation of the object is not in the form of new. Instead, you define a method that is used to create an object instance.
This article mainly introduces the PHP design mode of the Factory mode and singleton mode, a simple introduction of the Factory mode and the function of the singleton mode, and combined with the example form analysis of the factory model and the implementation and application of the singleton mode, the need for friends can refer to the following :
Factory mode: create appropriate objects for your needs
Class factory{ function construct ($name) { if (file_exists ('./') $name. '. Class.php ') { return new $name; } else{die (' not exist ');}}}
Single-case mode: creates an instance of only one object, does not allow the creation of instances, saves resources (such as database connections)
Class instance{public $val = ten; private static $instance; Private Function construct () {} Private Function Clone () {} //Set to static method can be called by class 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 can invoke clone () clone that is new to the object//$obj _two = Clone $obj _one; $obj _two = Instance::getinstance (), Echo $obj _two->val;echo ' <p> '; Var_dump ($obj _one, $obj _two);
The results of the operation are as follows:
20object (instance) [1] public ' val ' = + int 20object (instance) [1] public ' val ' = + int 20
Application: Database connection class (DB Access Oject)
Class mysqldb{Private $arr = Array (' port ' = = 3306, ' host ' = ' localhost ', ' username ' = ' root ', ' p Assward ' + ' root ', ' dbname ' = ' instance ', ' charset ' = ' utf8 '); Private $link; Static $instance; Private Function Clone () {} private Function construct () {$this->link = mysql_connect ($this->arr[' host '), $this-&G t;arr[' username '), $this->arr[' passward ') or Die (Mysql_error ()); mysql_select_db ($this->arr[' dbname ') or Die (' db error '); Mysql_set_charset ($this->arr[' CharSet '); } 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 ();