Singleton schema definition

This class is responsible for creating its own objects and ensuring that only a single object is created. This class provides a way to access its unique objects directly, without instantiating the objects of the class.

In C++, my personal understanding is that there is no need to own new class pointer, which on the one hand is convenient for programmers to manage their own memory, on the other hand can save system pointer, reduce the footprint of memory resources.

Implementation requirements of singleton pattern

In simple terms, a class must have a unique singleton class that can be called by other objects, so it doesn’t make sense.

Code implementation of singleton pattern

With the previous two definitions and implementation requirements understood, let’s take a look at how the singleton pattern can be cleverly utilized in C++.

First, take a look at the classes that use the singleton pattern

The header singleton class is defined as follows:

// Singleton pattern class
class Singleton
{
public:
	Singleton();
	~Singleton();
	void walk(a);

	static Singleton * getSingleton(a); // Returns a singleton pointer
private:
	static Singleton * m_Instance;// Singleton global pointer

	// Release static members at the end of the program
	class CGararge// Its only job is to delete in the destructorCSingletonAn instance of the {  
	public:  
		~CGararge()  
		{  
			// clear the memory of the singleton pointer
			if (Singleton::m_Instance)  
				deleteSingleton::m_Instance; }};static CGararge Garbo; // Define a static member whose destructor is called when the program terminates
};
Copy the code

It defines a singleton member pointer m_Instance or how to get a singleton pointer

Class CGarbage is also defined to release singleton Pointers

The next step is to implement the CPP file

Singleton::Singleton()
{
}

Singleton* Singleton::m_Instance = nullptr;

Singleton::~Singleton()
{
	cout << "It's destructed." << endl;
}

void Singleton::walk()
{
	cout << " I AM WALKING ALONE" << endl;
}

Singleton* Singleton::getSingleton()
{
	// Create a new one if it is not instantiated
	if(m_Instance==nullptr)
	{
		m_Instance = new Singleton();
	}
	return m_Instance;
}
Copy the code

The getSingleton() function is used to determine if a singleton exists. This ensures that the global pointer to the entire class takes up only one memory address, without requiring new every time

Let’s look at the test code

	Singleton * cur_Instance = Singleton::getSingleton();
	cur_Instance->walk();
	cout << "Old address" << cur_Instance << endl;
	Singleton * cur_NextInstance = cur_Instance->getSingleton();
	cur_NextInstance->walk();
	cout << "New address"<< cur_NextInstance << endl;
	int mm ;
	cin >> mm;
Copy the code

Run the program:

It is obvious that only one instance pointer is generated, which makes it easy to manage memory and does not consume memory resources

Now let’s see what happens to a regular class that doesn’t use the singleton pattern

The header file

class People
{
public:
	void walk(a);
};
Copy the code

Defines a walk method in a person’s class

implementation

void People::walk()
{
	cout << " I AM WALKING ALONE" << endl;
}

Copy the code

It’s very simple same thing as above

Let’s look at the test code

	People *p_people = new People();
	p_people->walk();
	People *p_nextPeople = new People();
	p_nextPeople->walk();
	cout << "Old address" << p_people << endl; 
    cout << "New address" <<  p_nextPeople << endl; 

	delete p_people;
	delete p_nextPeople;
Copy the code

The test code is also simple

New two Pointers and then execute walk and finally look at the address and free the memory

Take a look at the results:

Clear the produced two memory, if in a large project Need among different file reference classes, it is clear that for the program itself takes up memory As a burden, and it’s easy to cause the phenomenon of memory leak, so the advantage of the singleton pattern is obvious, can take up less memory as much as possible, and its easy to manage memory, I hope that through two examples, we can bring a little inspiration.