C ++ singleton design pattern

Singleton: so only generate a class object.

We can think about how to limit the number of class-generated objects do? We know that a target of generating at least two steps to open up the memory, call the constructor. We have no way to open up the memory limitations, but we can control the situation call the constructor, along this line of thought we have come Singleton pattern design ideas:

  • To shield objects generated interface that controls the constructor, we set the constructor private, let it not be called outside the class. Therefore, the constructor and copy constructor is private to the first step.
  • We want to provide a common interface to generate a unique object, when we designed this function to meet the two points, 1. class type can not be returned because the class type of the return will generate a temporary object, then we have the two objects , not so, we can cite the class pointer and optionally a class. 2. Interface to get rid of dependence on the object, otherwise never be able to generate the first object, so set it static, static objects do not belong, it can be called by the class.
  • This interface will be called many times, so we have to ensure that he was transferred many times but only produce an object, therefore I am Emei you set up a static member variable to hold only one class, each judge when you call, if for the first the first call is generated classes, as if the second call is returned directly category.

That we now write a simple class a singleton pattern according to this line of thought:

# include <iostream>
class master
{
private:
	master (char* name,bool sex)//构造函数
	{
		mname=new char[strlen(name+1)]();
		strcpy(mname,name);
		msex=sex;
	}
	master(const master&)//拷贝构造
	{}
	char *mname;
	bool msex;
	static master *pinstance;//静态成员变量,保存一个对象的地址
public:
	static master* getinstance(char* name,bool sex)//公共的接口
	{
		if(pinstance==NULL)//第一次则生成对象
		{
			pinstance=new master(name,sex);
		}
		return pinstance;//不是第一次则直接返回第一次生成的对象地址
	}
	void show()
	{
		std::cout<<"生成的对象名字为"<<mname<<std::endl;
	}
};
master* master::pinstance=NULL;//初始化静态成员
int main()
{
	master *pm1=master::getinstance("王麻子", true);//定义对象指针保存对象的地址
	master *pm2=master::getinstance("李四", true);
	master *pm3 = master::getinstance("王五", true);
	pm1->show();
	pm2->show();
	pm3->show();
}

We can see that the main function of which we applied for three objects, the program will generate three objects whether it is us, it is still only one object, we have to see through the print function, the results as shown below:

We can see that the system only generates the first object for us, Wang Mazi, the program directly address the return of Wang Mazi other objects are rendered, so to achieve a singleton pattern. But we can see that it is unsafe procedures, and inefficient use of this time we again generated a singleton object called lazy mode, also known as delay loading , it is a thread-safe mode.

Then we certainly can not expect to have a safe mode to achieve Singleton pattern, of course, there, I introduced the following for everyone starving mode, also known as greedy loaded , I believe we see the name to know what the meaning of it, not it is wrong to take preventive measures in advance to produce an object, the object will be generated before the main, so there is no thread unsafe. Specific code as follows:

# include <iostream>
class master
{
private:
	master (char* name,bool sex)//构造函数
	{
		mname=new char[strlen(name+1)]();
		strcpy(mname,name);
		msex=sex;
	}
	master(const master&)//拷贝构造
	{}
	char *mname;
	bool msex;
	static master *pinstance;//静态成员变量,保存一个对象的地址
public:
	static master* getinstance(char* name,bool sex)//公共的接口
	{
		return pinstance;//直接返回已经生成的对象地址,保证只有一个对象
	}
	void show()
	{
		std::cout<<"生成的对象名字为"<<mname<<std::endl;
	}
};
master* master::pinstance=new master("王麻子",true);//在主函数之前就已经申请好了对象,所以只会有王麻子这一个对象
int main()
{
	master *pm1=master::getinstance("张三", true);//定义对象指针保存对象的地址
	master *pm2=master::getinstance("李四", true);
	master *pm3 = master::getinstance("王五", true);
	pm1->show();
	pm2->show();
	pm3->show();
}

We can see that when a static member variable holds only one object initialization we have made it an object which generates Wang Mazi, so it is before the main function is generated, and whether you define it in a few main function inside objects, it will not generate, Wang Mazi address each time you return only to find objects in the application, we look at the results: 

Singleton pattern of this design is to, for any errors please point out Oh! Come on? ♀️

 

 

Published 54 original articles · won praise 8 · views 5323

Guess you like

Origin blog.csdn.net/qq_43411555/article/details/90341421