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

一个新手
Release: 2023-03-16 17:08:02
Original
2692 people have browsed it
//让该类在外界无法造对象 //让外界可以造一个对象,做一个静态方法返回对象 //在类里面通过让静态变量控制返回对象只能是一个。
Copy after login

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";
Copy after login

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();
Copy after login

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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!