Design Patterns: Singleton Pattern Notes

insert image description here

1. Singleton mode

  • a classOnly one object can be created, the design pattern of such a class is called the singleton pattern, which guaranteesin the systemthe classThere can only be one instance(andParent-child process sharing), a typical singleton class is the memory pool of C++STL
  • The basic design ideas of C++ singleton mode:
    • privatizationConstructor, delete the defaultcopy constructorandassignment operator overloadingPrevent objects from being created and copied directly
    • The memory resources of the singleton object can be released by the operating system, or the destructor can be customized to complete special operations

2. Two implementations of singleton mode

hungry man mode

  • Hungry singleton class in the programbefore entering the main functioncreates a unique instance of
//饿汉单例模式
class HungerSingleton
{
    
    
public:
	//定义一个可以访问单例对象的静态接口
	static HungerSingleton* Getinstance()
	{
    
    
		return &singleObj;
	}
private:
	//构造函数私有化,防止对象被直接创建
	HungerSingleton() {
    
     cout << "单例对象创建" << endl; }
	//删除拷贝接口,防止对象被拷贝
	HungerSingleton(const HungerSingleton& single) = delete;
	HungerSingleton& operator=(const HungerSingleton& single) = delete;

private:
	//定义静态区的HungerSingleton成员
	static HungerSingleton singleObj;
	//也可以定义成指针,初始化时在堆上创建
	//static HungerSingleton* singleObj;
};
//初始化类的静态成员
HungerSingleton HungerSingleton::singleObj;
//初始化时在堆上创建
//HungerSingleton * HungerSingleton::singleObj = new HungerSingleton;
  • HungerSingletonofstatic member variableyesan object (or pointer) of its own type,Categorystatic member variableThe initialization is completed before entering the main function. Since the constructor is privatized, objects of this class cannot be created during the running of the program.
  • Advantages of Hungry Singleton Mode:
    • becauseChild processes can only be created in the main function, so the singleton classThere are no thread safety issues, without competing for system resources with classes of other threads, inIn a multi-threaded high-concurrency environmentAble to perform tasks more efficiently
  • Disadvantages of the Hungry Singleton Pattern:
    • If there are multiple singleton classes in a program, weno control over their initialization order
    • The hungry singleton class will slow down the startup speed of the program, and even if the class is not used, an instance will be created, which may cause memory waste

lazy mode

  • lazy singleton class in the programAfter entering the main functionIt is up to subsequent code to decide whether to create an instance
//懒汉单例模式
class LazySingleton
{
    
    
public:
	//定义一个可以访问单例对象的静态接口
	static LazySingleton* Getinstance()
	{
    
    
		//若singleObj为空指针则创建单例对象
		if (singleObj == nullptr)
		{
    
    
			singleObj = new LazySingleton;
		}
		return singleObj;
	}
private:
	//构造函数私有化,防止对象被创建
	LazySingleton() {
    
     cout << "单例对象创建" << endl; }
	//删除拷贝接口,防止对象被拷贝
	LazySingleton(const LazySingleton& single) = delete;
	LazySingleton& operator=(const LazySingleton& single) = delete;

private:
	//定义成静态成员指针,初始化时在堆上创建
	static LazySingleton* singleObj;
};
//初始化时设置成空指针
LazySingleton * LazySingleton::singleObj = nullptr;
  • LazySingletonGetinstance()The class will create an instance when the member interface is called for the first time
  • Advantages of the lazy singleton pattern:
    • able to controlThe initialization order of various lazy singleton class objects, and only created when needed, avoiding memory waste
  • Disadvantages of the lazy singleton pattern:
    • There are thread safety issues in a multi-threaded environment, and locks are required
      insert image description here

Guess you like

Origin blog.csdn.net/weixin_73470348/article/details/132324257