Singleton mode
-
Demand proposals:
- In architecture design, some classes can only have one Single Instance in the whole system life cycle.
Such as: supermarket cash register system, a monitor, a scanning gun, a keyboard..
Question:
How to define a class so that it can only create one object at most?
- To control the number of objects in a class, the constructor must be hidden from the outside world.
-
Train of thought:
- Set the access property of the constructor to private
- Define instance and initialize it to NULL
-
When using objects, access the value of instance
- Null value: Create an object and mark it with instance
- Non-null values: Objects that return instance Tags
Programming Experiments: A Preliminary Study of Single Case Model
#include <iostream> using namespace std; class SObject { private: static SObject* c_instance; SObject& operator=(const SObject&); SObject(const SObject&); SObject() { } public: static SObject* GetInstance(); void print() { cout << "this = " << this << endl; } }; SObject* SObject::c_instance = NULL; SObject* SObject::GetInstance() { if( c_instance == NULL ) { c_instance = new SObject(); } return c_instance; } int main() { SObject* s = SObject::GetInstance(); SObject* s1 = SObject::GetInstance(); SObject* s2 = SObject::GetInstance(); s->print(); s1->print(); s2->print(); return 0; }
Output: this = 0x8069008 this = 0x8069008 this = 0x8069008
-
Existing problems
-
When the singleton mode is needed:
- The static member variable c_instance must be defined in the class used
- The static member function GetInstance() must be defined in the class used.
-
- Solution
The code related to the singleton pattern is extracted and the singleton class template is developed. When a singleton class is required, use the singleton class template directly
Programming Experiments: Singleton Class Template
Singleton.h
#ifndef _SINGLETON_H_ #define _SINGLETON_H_ template < typename T > class Singleton { private: static T* c_instance; public: static T* GetInstance(); }; template < typename T > T* Singleton<T>::c_instance = NULL; template < typename T > T* Singleton<T>::GetInstance() { if( c_instance == NULL ) { c_instance = new T(); } return c_instance; } #endif
main.cpp
#include <iostream> #include "Singleton.h" using namespace std; class SObject { private: friend class Singleton<SObject>; // Current classes need to use the singleton pattern SObject& operator=(const SObject&); SObject(const SObject&); SObject() { } public: void print() { cout << "this = " << this << endl; } }; int main() { SObject* s = Singleton<SObject>::GetInstance(); SObject* s1 = Singleton<SObject>::GetInstance(); SObject* s2 = Singleton<SObject>::GetInstance(); s->print(); s1->print(); s2->print(); return 0; }
Output: this = 0x8188008 this = 0x8188008 this = 0x8188008
Summary
- Singleton pattern is one of the most commonly used design patterns in development
- The application of the singleton pattern enables a class to have at most one object
- Code associated with singleton patterns can be abstracted into class templates
- Classes that need to use the singleton pattern use the singleton class template directly
The above content refers to Ditai Software College Series Courses, please protect the originality.