Explain the advantages and disadvantages, points for attention and usage scenarios of singleton mode


Knowing what you can’t do is more important than knowing what you can do.

Introduction of singleton mode:

Singleton, also known as singleton, is a common software design pattern. When applying this pattern, classes of singleton objects must ensure that only one instance exists. Most of the time, the whole system only needs to have one global object, which is helpful for us to coordinate the behavior of the whole system.

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

Implementation idea of singleton mode:

  • A class can return an object reference (always the same) and a method to get the instance (it must be a static method, usually using the name getInstance);
  • When calling this method, if the reference held by the class is not empty, the reference is returned. If the reference held by the class is empty, an instance of the class is created and the reference of the instance is given to the reference held by the class;
  • The constructor of this class is defined as a private method, so that the code in other places can’t instantiate the object of this class by calling the constructor of this class. Only the static method provided by the class can get the unique instance of the class.

Points for attention:

Singleton mode must be used with care in multithreaded, applications. If two threads call the creation method at the same time when the unique instance has not been created, they do not detect the existence of the unique instance at the same time, thus creating an instance at the same time. In this way, two instances are constructed, thus violating the principle of unique instances in singleton mode.

The solution to this problem is to provide a mutex for variables that indicate whether the class has been instantiated (although this reduces efficiency).


  1. In the singleton pattern, the singleton of an activity has only one instance, and all instantiations of the singleton class get the same instance. This prevents other objects from instantiating themselves and ensures that all objects access an instance
  2. Singleton pattern has a certain degree of scalability. The class controls the instantiation process by itself, and the class has the corresponding scalability in changing the instantiation process.
  3. Provides controlled access to a unique instance.
  4. Because there is only one object in the system memory, it can save system resources. When the objects need to be created and destroyed frequently, the singleton pattern can improve the performance of the system.
  5. Allow variable instances.
  6. Avoid multiple occupancy of shared resources.


  1. It is not suitable for changing objects. If objects of the same type always change in different use case scenarios, single instance will cause data errors and cannot save each other’s state.
  2. Because there is no abstract layer in the simple profit model, it is difficult to extend the singleton class.
  3. To some extent, it violates the “single responsibility principle”.
  4. Abusing singleton will bring some negative problems. For example, in order to save resources, the database connection pool object is designed as a singleton class, which may lead to connection pool overflow due to too many programs sharing connection pool objects. If the instantiated object is not used for a long time, the system will consider it as garbage and be recycled, which will lead to the loss of object state.

Precautions for use:

  1. You cannot use reflection mode to create singleton, otherwise a new object will be instantiated
  2. Pay attention to thread safety when using lazy singleton mode
  3. The construction methods of both empty singleton pattern and lazy singleton pattern are private, so they can’t be inherited. Some singleton patterns can be inherited (for example, registered schema)

Applicable scenarios:

Singleton mode allows only one object to be created, so it can save memory and speed up object access. Therefore, the object needs to be used in common occasions, such as multiple modules using the same data source to connect objects, etc. For example:

  • Objects that need to be instantiated frequently and then destroyed.
  • Objects that take too much time or resources to create, but are often used.
  • A stateful tool class object.

    -An object that frequently accesses a database or file.

The following are classic usage scenarios of singleton mode:

  1. In the case of resource sharing, performance or loss caused by resource operation can be avoided. As in the above log file, apply the configuration.
  2. In the case of controlling resources, the communication between resources is convenient. Such as thread pool.

Application scenarios:

1. External resources: each computer has several printers, but only one printer spooler can be used to avoid two print jobs being output to the printer at the same time. Internal resources: most software has one (or more) property files to store the system configuration. Such a system should have an object to manage these property files

2. The task manager of windows is a very typical singleton mode (this is very familiar with it). Think about it, can you open two windows task managers? If you don’t believe it, try it yourself~

3. The recycle bin of windows is also a typical single application. In the whole system running process, the recycle bin has been maintaining only one instance.

4. The counter of website is usually implemented in single instance mode, otherwise it is difficult to synchronize.

5. The log application of the application program is generally implemented in the singleton mode. This is generally because the shared log file is always open, because only one instance can operate, otherwise the content is not easy to be appended.

6. In general, singleton mode is also applied to read configuration objects of web applications. This is because configuration files are shared resources.

7. The design of database connection pool generally adopts singleton mode, because database connection is a kind of database resource. The use of database connection pool in the database software system is mainly to save the efficiency loss caused by opening or closing the database connection. This kind of efficiency loss is still very expensive, because how to use singleton mode to maintain can greatly reduce this loss.

8. The design of multithreaded thread pool generally adopts singleton mode, which is because the thread pool should be convenient to control the threads in the pool.

9. The file system of the operating system is also a concrete example of the implementation of large singleton mode. An operating system can only have one file system.

10. Httpapplication is also a typical application of unit example. be familiar with ASP.Net (IIS) should know that httpapplication is also singleton mode, and all httpmodules share one httpapplication instance