PHP singleton pattern

Singleton pattern is a commonly used software design pattern. In its core structure it contains only a special class called a singleton. The singleton pattern can ensure that in the system, a class that applies this pattern has only one instance of a class. That is, a class has only one object instance

For example, in a server program, the configuration information of the server is stored in a file, and the configuration data is uniformly read by a singleton object, and then other objects in the service process obtain the configuration information through the singleton object. This approach simplifies configuration management in complex environments.


For some classes in the system it is important to have only one instance, for example, there can be multiple printing tasks in a system, but only one working task; a system can only have one window manager or file system ; A system can only have one timing tool or ID (serial number) generator. For example, in Windows, only one task manager can be opened. If the mechanism is not used to uniqueize the window object, multiple windows will pop up. If the content displayed in these windows is exactly the same, it will be a duplicate object and a waste of memory resources; if the content displayed in these windows is inconsistent, it means that at a certain moment The system has multiple states, which are inconsistent with the actual situation, and will also cause misunderstandings to users, who do not know which one is the real state. So sometimes it is very important to ensure the uniqueness of an object in the system, that is, only one instance of a class. [3]
How to ensure that a class has only one instance and this instance is easy to access? Defining a global variable ensures that the object is always accessible, but it doesn't prevent us from instantiating multiple objects. A better solution is to make the class itself responsible for keeping its only instance. This class can guarantee that no other instance is created, and it can provide a method to access the instance. This is the pattern motivation of the singleton pattern.


Obviously, there are three main points of the singleton pattern; one is that a class can only have one instance; the other is that it must create this instance by itself; the third is that it must provide this instance to the entire system by itself.
From the perspective of specific implementation, it is the following three points: firstly, the singleton pattern class only provides a private constructor; secondly, the class definition contains a static private object of the class; thirdly, the class provides a static public The function is used to create or get its own static private object.
In the object diagram below, there is a "Singleton Object", and "Customer A", "Customer B" and "Customer C" are the three customer objects of the singleton object. As you can see, all client objects share a singleton object. And from the connection line from the singleton object to itself, it can be seen that the singleton object holds a reference to itself.

class test {
 private  static  $_instance ; // Save the private static member variable of the class instance
//Define a private constructor to ensure that the singleton class cannot be instantiated by the new keyword, but only by itself 
private  final  function __construct() {
 echo 'test __construct' ;
}
// Define a private __clone() method to ensure that the singleton class cannot be copied or cloned 
private  function __clone() {}
 public  static  function getInstance() {
 // Check if the class is instantiated 
if ( ! (self:: $ _instance instanceof self) ) {
self::$_instance = new test();
}
return self::$_instance;
}
}
// Call the singleton class 
test::getInstance();

advantage

There is only one object of this class in the system memory, which saves system resources. For some objects that need to be frequently created and destroyed, using the singleton mode can improve system performance.

shortcoming

When you want to instantiate a singleton class, you must remember to use the corresponding method to get the object instead of using new, which may cause trouble to other developers, especially when the source code is not visible.

Applications

  • Objects that need to be created and destroyed frequently;
  • Objects that take too much time or consume too many resources when creating objects, but are often used;
  • tool class object;
  • Objects that frequently access databases or files.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324498003&siteId=291194637