1. The design patterns in the software field provide developers with an effective way to use expert design experience. The design model utilizes the important features of object-oriented programming languages: encapsulation, inheritance, and polymorphism. Understanding the essence of the design model is a long process that requires a lot of practical experience.
2. When only one instance of the class can be created in the application, we use Singleton Pattern ). It protects the creation process of a class to ensure that only one instance is created. It is implemented by setting the class constructor to private. To obtain the instance of the class, a singleton class can provide a method, such as getInstance, to return the instance of the class. This method is the only method that can be used to create an instance by using the sequence class. For example:
C ++ code:
//Singleton.h class Singleton { public: static Singleton* GetInstance(); private: Singleton() {} static Singleton *m_pInstance; }; //Singleton.cpp Singleton* Singleton::m_pInstance = NULL; Singleton* Singleton::GetInstance() { if(m_pInstance == NULL) m_pInstance = new Singleton(); return m_pInstance; } //Singleton.hclass Singleton {public: static Singleton* GetInstance();private: Singleton() {} static Singleton *m_pInstance;};//Singleton.cppSingleton* Singleton::m_pInstance = NULL;Singleton* Singleton::GetInstance(){ if(m_pInstance == NULL) m_pInstance = new Singleton(); return m_pInstance;}
This class has the following features:
◆ Its constructor is private, so that you cannot create instances of this class elsewhere.
◆ It has a unique static pointer m_pInstance and is private.
◆ It has a public function that can obtain this unique instance and create it as needed.
3. But it still has a problem. When will the space pointed to by m_pInstance be released? The more serious problem is, when will the instance's destructor be executed? If necessary operations are required in the class destructor, such as closing the file and releasing external resources, the code shown above cannot meet this requirement. We need a method to delete the instance normally.
The proper method is to let the class delete itself when appropriate.
At the end of the program, the system will automatically analyze all global variables. In fact, the system will analyze the static member variables of all classes, just like these static members are also global variables. With this feature, we can define such a static member variable in the C ++ Singleton mode class, the only task is to delete the instance of the singleton class in the destructor. The CGarbo class in the following code (Garbo stands for spam workers ):
Class Singleton {// other members public: static Singleton * GetInstance (){...} Private: Singleton () {}; static Singleton * m_pInstance; class CGarbo // Its only job is to delete the CSingleton instance {public: ~ In the destructor :~ CGarbo () {if (Singleton: m_pInstance) delete Singleton: m_pInstance ;}; static CGarbo Garbo; // defines a static member. At the end of the program, the system will call its destructor} class Singleton {// other members public: static Singleton * GetInstance (){...} Private: Singleton () {}; static Singleton * m_pInstance; class CGarbo // Its only job is to delete the CSingleton instance {public: ~ In the destructor :~ CGarbo () {if (Singleton: m_pInstance) delete Singleton: m_pInstance ;}; static CGarbo Garbo; // defines a static member. At the end of the program, the system will call its destructor}
Class CGarbo is defined as the private embedded class of CSingleton to prevent this class from being abused elsewhere.
At the end of the program running, the system calls the Garbo destructor, a static member of CSingleton. This destructor deletes the unique instance of a single instance.
Releasing a singleton object in this way has the following features:
◆ Define proprietary Nested classes within the singleton class.
◆ Private Static members dedicated for release are defined in the singleton class.
◆ Use the program to analyze the global variables at the end and select the final release time.
◆ Code using the C ++ Singleton mode does not require any operation and does not need to care about the release of objects.