[C + + design mode] singleton mode

Posted by jennatar77 on Fri, 14 Jan 2022 13:43:11 +0100

1. What is singleton mode?

Singleton mode is an object creation mode. Using singleton mode can ensure that only unique instance objects are generated for a class. That is, there is only one instance object of this class in the whole program space.    

Note the following:

  • A singleton class can only be instantiated by one object.

  • A singleton class must provide its own instantiation object.

  • A singleton class must provide an interface that can access a uniquely instantiated object.

Singleton mode is divided into lazy and hungry.

Lazy man: so the name implies that you will not instantiate a class unless you have to, that is, you will instantiate an object when you use a class instance for the first time. When the number of visits is small or even may not be visited, it is implemented by lazy people, which is to trade time for space.

Hungry man: if you're hungry, you must be hungry. Therefore, instantiation is performed when a singleton class is defined. When the number of accesses is large or there are many threads that may be accessed, the hungry man implementation can achieve better performance. This is space for time.

2. Why use singleton mode?

  • Between multiple threads, such as initializing socket resources once; For example, servlet environment, sharing the same resource or operating the same object
  • Use global variables in the whole program space to share resources

  • In large-scale systems, in order to consider performance, it is necessary to save object creation time and so on.

Because Singleton pattern can ensure that only unique instance objects are generated for a class, Singleton pattern is useful in these cases.

3. Implementation of singleton steps?

  1. Constructor privatization
  2. Provides a global static method
  3. Define a static pointer in the class to point to the static variable pointer of the variable of this class.

4. Basic single case

4.1 lazy single case

#include <iostream>
using namespace std;

//Lazy style
class SingeIton
{
//Constructor privatization
private:
    SingeIton()
    {
        m_singer = NULL;
        m_count = 0;
        cout<<"SingeIton...do"<<endl;
    }

public:
    //Class
    static SingeIton *getInstance()
    {
        if(m_singer == NULL)
        {
            m_singer = new SingeIton;
        }
        return m_singer;
    }

    //Provides a global static method
    static void printT()
    {
        cout<<"m_count:"<<m_count<<endl;
    }

private:
    static SingeIton *m_singer;
    static int m_count;
};

SingeIton *SingeIton::m_singer = NULL;//Lazy, no singleton created;
int SingeIton::m_count = 0;

int main()
{
    cout<<"Demonstrate lazy style"<<endl;
    SingeIton *p1 = SingeIton::getInstance();//Created only when used
    SingeIton *p2 = SingeIton::getInstance();
    if(p1 != p2)
    {
        cout<<"Not the same object"<<endl;
    }
    else
    {
        cout<<"Is the same object"<<endl;
    }

    p1->printT();
    p2->printT();

    system("pause");
    return 0;
}

The results show that:

Demonstrate lazy style
SingeIton...do
 Is the same object
m_count:0
m_count:0

4.2 hungry Han mode

#include <iostream>
using namespace std;

//Hungry Han style
class SingeIton
{
//Constructor privatization
private:
    SingeIton()
    {
        m_singer = NULL;
        m_count = 0;
        cout<<"Constructor SingeIton...do"<<endl;
    }

public:
    //Class
    static SingeIton *getInstance()
    {
        return m_singer;
    }

    static SingeIton FreeInstance()
    {
        if(m_singer != NULL)
        {
            delete m_singer;
            m_singer = NULL;
            m_count = 0;
        }
    }

    //Provides a global static method
    static void printT()
    {
        cout<<"m_count:"<<m_count<<endl;
    }

private:
    static SingeIton *m_singer;
    static int m_count;
};

SingeIton *SingeIton::m_singer = new SingeIton;//Whether you create an instance or not, create a new instance
int SingeIton::m_count = 0;

int main()
{
    cout<<"****Demonstrate hungry Chinese style****"<<endl;
    SingeIton *p1 = SingeIton::getInstance();//Created only when used
    SingeIton *p2 = SingeIton::getInstance();
    if(p1 != p2)
    {
        cout<<"Not the same object"<<endl;
    }
    else
    {
        cout<<"Is the same object"<<endl;
    }

    p1->printT();
    p2->printT();
    SingeIton::FreeInstance();
    SingeIton::FreeInstance();


    system("pause");
    return 0;
}

The results show that:

****Demonstrate hungry Chinese style****
Is the same object
m_count:0
m_count:0

Topics: C++ Singleton pattern