Singleton mode is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode can ensure that there is only one instance of a class in the system, and the instance is easy to be accessed by the outside world, thus it is convenient to control the number of instances and save system resources. Singleton mode is the best solution if you want to have only one object for a class in the system.
Motivation:
For some classes in the system, only one instance is important, for example, there can be multiple print tasks in a system, but only one task is working; a system can have only one window manager or file system; A system can have only one timing tool or ID (ordinal) generator. You can only open one task Manager in Windows. If you do not use the mechanism to unique window objects, will pop up more than one window, if these windows display exactly the same content, it is a duplicate object, wasting memory resources, if these windows display inconsistent content, it means that in a moment the system has multiple states, and the actual discrepancy, but also to the user misunderstanding, I don't know which one is the real state. Therefore, it is sometimes important to ensure that an object in the system is unique, that only one instance of a class is available. How do you ensure that there is only one instance of a class and that this instance is easy to access? Defining a global variable ensures that an object can be accessed at any time, but it does not prevent us from instantiating multiple objects. A better solution is to have the class itself responsible for preserving its only instance. This class guarantees that no other instance is created, and it can provide a way to access the instance. This is the pattern motive of the singleton pattern. Important: (1) A class can have only one instance-the class definition contains a static private object of that class, and (2) It must create the instance itself-the singleton class only provides a private constructor, and (3) It must provide this instance to the system itself-The class provides a static public function to create or get a static private object of its own. Advantages:first, instance controlSingleton mode prevents other objects from instantiating copies of their own singleton objects, ensuring that all objects have access to unique instances. Ii. flexibility because classes control the instantiation process, classes can flexibly change the instantiation process. Cons: First, although the amount of overhead is very small, there is still some overhead required to check if an instance of the class exists each time the object requests a reference. This problem can be resolved by using static initialization. Ii. possible development confusion when using singleton objects, especially those defined in a class library, developers must remember that they cannot instantiate an object using the New keyword. Because library source code may not be accessible, application developers may unexpectedly find themselves unable to instantiate this class directly. Third, the object lifetime cannot resolve the problem of deleting a single object. In a language that provides memory management, such as a. NET framework-based language, only a singleton class 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 results in a floating reference in a singleton class.
classcsingleton{Private: Csingleton ()//The constructor is private { } Public: StaticCsingleton *getinstance () {StaticCsingleton *m_pinstance; if(m_pinstance = = NULL)//determines whether the first call isM_pinstance =NewCsingleton (); returnm_pinstance; }};
Single-Case mode