What Is Single-Instance Storage?
Singleton pattern is a commonly used software design pattern for creation types. The class created by the singleton pattern method has only one instance in the current process (depending on the need, it may be a singleton in a thread, such as using the same instance in the thread context only)
- In mathematics and logic, a singleton is defined as a "set with only one element".
- The original definition of the singleton pattern appeared in Design Patterns (Addison Wesley, 1994): "Guarantee a class with only one instance and provide a global access point to it."
- The singleton pattern definition in Java: "A class has one and only one instance, and instantiates itself to the entire system."
- Java singleton pattern example
public class Singleton { private Singleton () { } private static volatile Singleton instance = null; public static Singleton getInstance () { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton (); } } } return instance; } }
- Usually the singleton pattern is in
- For some classes in the system, only one instance is important. For example, there can be multiple print tasks in a system, but there can only be one working task; a system can only have a 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 you do not use a mechanism to uniqueize the window object, multiple windows will pop up. If the contents displayed by these windows are completely the same, it will be a duplicate object and waste memory resources. The system has multiple states, which are inconsistent with the actual situation, and will also cause misunderstanding to the user. I don't know which one is the real state. So sometimes it is important to ensure the uniqueness of an object in the system, that is, only one instance of a class. [2]
- Obviously there are three main points of the singleton pattern; one is that a class can only have one instance; the second 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: one is that the singleton pattern class only provides private constructors; the other is that the class definition contains a static private object of the class; 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 three customer objects of the singleton object. As you can see, all client objects share a singleton object. And from the connection line of the singleton object to itself, it can be seen that the singleton object holds a reference to itself.
- some
Singleton pattern advantages
- First, instance control
- The singleton pattern prevents other objects from instantiating copies of their own singleton objects, ensuring that all objects have access to a unique instance.
- Second, flexibility
- Because classes control the instantiation process, classes have the flexibility to change the instantiation process.
Singleton pattern disadvantages
- I. overhead
- Although small in number, it will still require some overhead if an instance of the class is checked for each object request reference. This problem can be solved by using static initialization.
- Possible development confusion
- When using singleton objects (especially those defined in the class library), developers must remember that they cannot use the new keyword to instantiate objects. Because library source code may not be accessible, application developers may accidentally find themselves unable to instantiate this class directly.
- Third, the object lifetime
- The problem of deleting a single object cannot be solved. In languages that provide memory management (such as languages based on the .NET Framework), only singleton classes can cause an instance to be deallocated because it contains a private reference to the instance. In some languages (such as C ++), other classes can delete object instances, but this can cause floating references in singleton classes. [2]