In the previous article "In-depth analysis of the template method pattern in PHP" we introduced the template method pattern in PHP. The following article will take you to understand the singleton in the PHP design pattern. model.
#The singleton pattern is definitely ranked first among commonly used and frequently asked interview design patterns. On the one hand, it is simple enough and can be explained in a few words. On the other hand, it is complex enough. Its implementation is not only in one form, but also needs to consider the issue of multi-thread locking in asynchronous languages such as Java. So during the interview, don't think that the interviewer will relax when he asks the question about the singleton model. This model can really be deep or shallow, and it can extremely reflect the level of a developer. Because as long as you work for a period of time, you will inevitably come into contact with this model.
GoF definition: Ensure that a class has only one instance and provide a global access point to access it.
GoF class diagram
##Code implementation
class Singleton { private static $uniqueInstance; private $singletonData = '单例类内部数据'; private function __construct() { // 构造方法私有化,外部不能直接实例化这个类 } public static function GetInstance() { if (self::$uniqueInstance == null) { self::$uniqueInstance = new Singleton(); } return self::$uniqueInstance; } public function SingletonOperation(){ $this->singletonData = '修改单例类内部数据'; } public function GetSigletonData() { return $this->singletonData; } }
$singletonA = Singleton::GetInstance(); echo $singletonA->GetSigletonData(), PHP_EOL; $singletonB = Singleton::GetInstance(); if ($singletonA === $singletonB) { echo '相同的对象', PHP_EOL; } $singletonA->SingletonOperation(); // 这里修改的是A echo $singletonB->GetSigletonData(), PHP_EOL;
singletonB is exactly the same object.
The company is getting bigger and bigger, but we only have one copy (single instance class) of all company rosters, which is stored in our OA system. What I'm afraid of is that confusion will arise after each department has its own roster. For example, if the update is not timely, newly joined or resigned employees from other departments will be missed. Other departments can view the entire roster when needed, or create and modify their own department's sections based on the entire roster. But in the OA system, what they actually modify is the contents of the general roster. What everyone maintains is actually the only real roster stored in the OA system server
Full code: https://github.com/zhangyue0503/designpatterns-php/blob/master/21.singleton/source/singleton.php
Cache class diagram
##Full source code: https://github.com/ zhangyue0503/designpatterns-php/blob/master/21.singleton/source/singleton-http.phpDescription<?php class HttpService{ private static $instance; public function GetInstance(){ if(self::$instance == NULL){ self::$instance = new HttpService(); } return self::$instance; } public function Post(){ echo '发送Post请求', PHP_EOL; } public function Get(){ echo '发送Get请求', PHP_EOL; } } $httpA = new HttpService(); $httpA->Post(); $httpA->Get(); $httpB = new HttpService(); $httpB->Post(); $httpB->Get(); var_dump($httpA == $httpB);Copy after login
- Is it still very simple? I won’t talk more about this form of singleton here. Let’s talk about several other forms of singletons
In static languages such as Java , static variables can be directly new objects, in the declaration
instance = new HttpService();. In this way, the GetInstance() method can be omitted, but this static variable will be directly instantiated and occupy memory regardless of whether it is used or not. This kind of singleton is called the hungry Chinese singleton pattern.
- Our code and examples are obviously not Hungry Man style, this form is called Lazy Man style. You have to actively use GetInstance() to get it, and then I will create the object.
- Lazy style In multi-threaded applications, such as Java multi-threading or after using swoole in PHP, there will be a problem of repeated creation, and the objects created multiple times are not the same object. At this time, double detection is generally used to ensure that there is only one global object. You can find the specific code yourself. There will be no problem with the Hungry Han style. The Hungry Han style itself has already assigned a value to the static attribute and will not change again.
- Another way is to create static inner classes. This is rarely seen, and its resource utilization rate is high. Put the static variable inside the method so that the static variable becomes a variable within the method instead of a variable in the class. It allows a singleton object to call its own static methods and properties.
Original address: https://juejin.cn/post/6844903990585458702
Author: Hardcore Project Manager
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Let's talk about the singleton pattern in PHP. For more information, please follow other related articles on the PHP Chinese website!