Home  >  Article  >  Backend Development  >  The difference between factory mode and singleton mode in PHP design patterns

The difference between factory mode and singleton mode in PHP design patterns

一个新手
一个新手Original
2017-10-18 09:07:562704browse
//让该类在外界无法造对象
//让外界可以造一个对象,做一个静态方法返回对象
//在类里面通过让静态变量控制返回对象只能是一个。

There are three main points of singleton mode:

  1. First, a class can only have one instance;

  2. The second is that it must create this instance by itself;

  3. The third is that it must provide this instance to the entire system by itself.

Why use PHP singleton mode

  1. 1. The application of PHP is mainly in the database application. In an application There will be a large number of database operations. When developing in an object-oriented way, if you use the singleton mode, you can avoid a large number of new operations consuming resources, and you can also reduce database connections so that too many connections are less likely to occur.

  2. 2. If a class is needed to globally control certain configuration information in the system, it can be easily implemented using the singleton mode. For this, please refer to the FrontController part of the zend Framework.

  3. 3. In a page request, it is easy to debug, because all the code (such as database operation class db) is concentrated in one class, and we can set hooks in the class, Output logs to avoid var_dump and echo everywhere.

class cat
{
    public $name;
    private function __construct()
    {

    }
    static $temp;
    static function new_ob()
    {
        if(empty(self::$temp)){
           self::$temp = new cat();
        }
        return self::$temp;
    }
    function  speak(){
        return "喵喵喵";
    }
}
//$mao = new cat();


//如果没有猫就造一个,然后等于新的xxx,如果有,直接等于。达到限制的目的。
//$mao;
//if (empty($mao)){
//    $mao = new cat();
//}
//    $xxx = $mao;

$mao = cat::new_ob();
$mao2 = cat::new_ob();
//给1猫改名字,2猫的名字输出为1猫改的名字。此为单例。
$mao->name = "py";

2. Simple factory pattern

  • ①Abstract base class: Define some abstract methods in the class to Implement

  • in the subclass ② Subclass inherited from the abstract base class: Implement the abstract method in the base class

  • ③ Factory class: Used to instantiate all corresponding subclasses

abstract class jsq
{
    public $a;
    public $b;

    function yunsuan(){
    }
}
//此为拓展
class jiafa extends jsq
{
    function  yunsuan()
    {
        //parent::yunsuan(); // TODO: Change the autogenerated stub
        return $this ->a+$this->b;
    }
}
class  jianfa extends jsq
{
    function yuansuan(){
        return $this->a-$this->b;
    }
}
class factory
{
   static function create($x){
       switch ($x){
           case "+":
               return new jiafa();
               break;
           case "-":
               return new jianfa();
               break;
       }
   }
}
//$j1 = new jiafa();
//$j1->a = 1;
//$j1->b = 2;
//$j1->yunsuan();

//尽量避免实例化对象,采用静态方法获取。
$jf = factory::create("+");
$jf->a = 1;
$jf->b = 2;
echo $jf->yunsuan();

The above is the detailed content of The difference between factory mode and singleton mode in PHP design patterns. 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