What is singleton pattern?

藏色散人
Release: 2023-04-05 20:36:02
forward
4910 people have browsed it



The Singleton pattern is a commonly used software design pattern. It contains only one special class called a singleton class in its core structure. The singleton mode can ensure that there is only one instance of a class in the system and that the instance is easy to access from the outside world, thereby facilitating control of the number of instances and saving system resources. If you want only one object of a certain class to exist in the system, the singleton pattern is the best solution.

How to ensure that a class has only one instance and that this instance is easy to access? Defining a global variable ensures that the object can be accessed at any time, but it does not prevent us from instantiating multiple objects. A better solution is to make the class itself responsible for saving its only instance. This class guarantees that no other instances are created, and it provides a method to access the instance. This is the pattern motivation behind the singleton pattern.

For example, during the development process of php, we created a db class (database operation class), then we hope that a database in a php file can only be connected once and only one database is needed in a php file. Object! Because connecting to the database multiple times will greatly reduce the execution efficiency of PHP. It will also bring huge system overhead!

Use singleton mode to encapsulate your database

<?php
class db
{
//使用一个静态变量记录db对象初始化时为null
public static $db = null;
/* 私有构造函数是类无法完成外部的调用
* 意味着您将无法使用 $xx = new db();
*/
private function __construct(){
echo &#39;连接数据库....&#39;;
}
/* 
* 使用静态方法去获取数据对象
* 获取时先判断db对象是否已经存在,如果存在则直接返回db对象反正则创建这个对象
*/
public static function getInstance(){
if(self::$db == null){
self::$db = new db();
}
return self::$db;
}
public function query($sql){
echo &#39;执行sql命令&#39;;
}
public function __destruct(){
echo &#39;关闭数据库连接....&#39;;
}
}
$db = db::getInstance();
$db1 = db::getInstance();
$db->query(&#39;test&#39;);
$db2 = db::getInstance();
//输出 : 连接数据库....执行sql命令关闭数据库连接....
Copy after login

//You can see that no matter how many times we obtain the db object, although their names are different, they all represent the same object! This implements the singleton pattern!



The above is the detailed content of What is singleton pattern?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:hcoder.net
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
Popular Tutorials
More>
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!