


//让该类在外界无法造对象 //让外界可以造一个对象,做一个静态方法返回对象 //在类里面通过让静态变量控制返回对象只能是一个。
There are three main points of singleton mode:
First, a class can only have one instance;
-
The second is that it must create this instance by itself;
The third is that it must provide this instance to the entire system by itself.
Why use PHP singleton mode
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. 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. 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!

如何在PHP后端功能开发中合理应用设计模式?设计模式是一种经过实践证明的解决特定问题的方案模板,可以用于构建可复用的代码,在开发过程中提高可维护性和可扩展性。在PHP后端功能开发中,合理应用设计模式可以帮助我们更好地组织和管理代码,提高代码质量和开发效率。本文将介绍常用的设计模式,并给出相应的PHP代码示例。单例模式(Singleton)单例模式适用于需要保

如何通过编写代码来学习和运用PHP8的设计模式设计模式是软件开发中常用的解决问题的方法论,它可以提高代码的可扩展性、可维护性和重用性。而PHP8作为最新版的PHP语言,也引入了许多新特性和改进,提供更多的工具和功能来支持设计模式的实现。本文将介绍一些常见的设计模式,并通过编写代码来演示在PHP8中如何运用这些设计模式。让我们开始吧!一、单例模式(Sing

本篇文章给大家带来了关于golang设计模式的相关知识,其中主要介绍了职责链模式是什么及其作用价值,还有职责链Go代码的具体实现方法,下面一起来看一下,希望对需要的朋友有所帮助。

随着数据的增长和复杂性的不断提升,ETL(Extract、Transform、Load)已成为数据处理中的重要环节。而Go语言作为一门高效、轻量的编程语言,越来越受到人们的热捧。本文将介绍Go语言中常用的ETL设计模式,以帮助读者更好地进行数据处理。一、Extractor设计模式Extractor是指从源数据中提取数据的组件,常见的有文件读取、数据库读取、A

单例模式是一种常见的设计模式,它在系统中仅允许创建一个实例来控制对某些资源的访问。在 Go 语言中,实现单例模式有多种方式,本篇文章将带你深入掌握 Go 语言中的单例模式实现。

设计模式的六大原则:1、单一职责原则,其核心就是控制类的粒度大小、将对象解耦、提高其内聚性;2、开闭原则,可以通过“抽象约束、封装变化”来实现;3、里氏替换原则,主要阐述了有关继承的一些原则;4、依赖倒置原则,降低了客户与实现模块之间的耦合;5、接口隔离原则,是为了约束接口、降低类对接口的依赖性;6、迪米特法则,要求限制软件实体之间通信的宽度和深度。

随着JavaScript的不断发展和应用范围的扩大,越来越多的开发人员开始意识到设计模式和最佳实践的重要性。设计模式是一种被证明在某些情况下有用的软件设计解决方案。而最佳实践则是指在编程过程中,我们可以应用的一些最佳的规范和方法。在本文中,我们将探讨JavaScript中的设计模式和最佳实践,并提供一些具体的代码示例。让我们开始吧!一、JavaScript中

探索Java开发中的设计模式经验与建议设计模式是软件开发中用于解决特定问题的一种面向对象的可复用解决方案。在Java开发中,设计模式是很重要的一部分,它能够提高代码的可读性和可维护性,并且能够加速开发过程。通过运用设计模式,开发人员可以更好地组织和管理代码,同时也能够避免一些常见的开发错误。在Java开发中,有很多常用的设计模式,如单例模式、工厂模式、观察者


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!
