Home>Article> What are the advantages of singleton pattern?

What are the advantages of singleton pattern?

Guanhui
Guanhui Original
2020-06-28 17:02:07 10265browse

Advantages of singleton mode: 1. It can ensure that all objects access the only instance; 2. Because the class controls the instantiation process, the class can flexibly change the instantiation process; 3. Because there is only one instance, it reduces Memory overhead and system performance overhead.

What are the advantages of singleton pattern?

#The singleton pattern is one of the simplest forms of design patterns. The purpose of this pattern is to make an object of a class the only instance in the system. To achieve this, you start by instantiating it on the client side. Therefore, it is necessary to use a mechanism that only allows the generation of unique instances of the object class, "blocking" all access to the object that is intended to be generated. Use factory methods to limit the instantiation process. This method should be a static method (class method), because there is no point in having an instance of the class generate another unique instance.

Singleton mode motivation

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 there can only be one working task; a system can only have one window manager or file system; and 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 you do not use a mechanism to uniqueize window objects, multiple windows will pop up. If the contents displayed in these windows are exactly the same, they are duplicate objects, which wastes memory resources; if the contents displayed in these windows are inconsistent, it means that at a certain moment The system has multiple states, which are inconsistent with reality and can 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, there can only be one instance of a class. [2]

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.

Key points of singleton mode

Obviously there are three main points of singleton mode; first, a class can only have one instance; second, it must create this instance by itself ; Third, it must provide this instance to the entire system by itself.

From a specific implementation perspective, there are three points: first, the singleton mode class only provides private constructors; second, the class definition contains a static private object of the class; third, the class Provides a static public function for creating or obtaining 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 customer objects share a singleton object. And it can be seen from the connection line from the singleton object to itself that the singleton object holds a reference to itself.

Some resource managers are often designed in singleton mode.

In a computer system, the resources that need to be managed include software external resources. For example, each computer can have several printers, but can only have one Printer Spooler to prevent two print jobs from being output to the printer at the same time. Each computer can have several fax cards, but only one software should be responsible for managing the fax cards to avoid the situation where two fax jobs are transferred to the fax card at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to prevent one communication port from being called by two requests at the same time.

The resources that need to be managed include software internal resources. For example, most software has one (or even multiple) properties files to store system configurations. Such a system should have an object manage a properties file.

The internal resources of the software that need to be managed also include components that are responsible for recording the number of visitors to the website, components that record internal events and error messages of the software system, or components that check the performance of the system. These components must be managed centrally, and multiple components cannot be created.

These resource manager components must have only one instance, which is the first; they must initialize themselves, which is the second; and the entire system is allowed to access themselves, which is the third. Therefore, they all meet the conditions of the singleton pattern and are applications of the singleton pattern.

Recommended tutorial: "PHP"

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

Statement:
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