Simple interest model

definition:

    Guarantees that there is only one instance of a class, and provides a global access point to it.

Main solution:

    A globally used class is frequently created and destroyed.

When to use:

    When you want to control the number of instances and save system resources.

How to solve:

    Determine whether the system already exists a singleton, if so, return it, if not, create it

Key code:

   Constructor is private


There are about two ways to achieve simple interest: lazy and hungry:

   Lazy: As the name implies, the class will not be instantiated unless it is absolutely necessary, that is to say, it will be instantiated when the class instance is used for the first time, so the above classic methods are classified as lazy implementation;

  Hungry man: If you are hungry, you must be hungry. So it is instantiated when the singleton class is defined.

Features and options:

    Due to the need for thread synchronization, when the amount of access is relatively large, or when there are many threads that may be accessed, the implementation of Hungry Han can achieve better performance. This is space for time.

   When the number of visits is small, the lazy person is implemented. This is time for space.

//Lazy general implementation: not thread-safe, the instance pointer returned by getInstance needs to be deleted  
class Singleton  
{  
public:  
    static Singleton* getInstance();  
    ~Singleton(){}  
  
private:  
    static Singleton* m_pSingleton;  
    Singleton(){}      
    Singleton(const Singleton& obj) = delete; // explicitly reject  
    Singleton& operator=(const Singleton& obj) = delete; // explicitly reject  
};  
  
Singleton* Singleton::m_pSingleton = NULL;  
  
Singleton* Singleton::getInstance()  
{  
    if(m_pSingleton == NULL)  
    {  
        m_pSingleton = new Singleton;  
    }  
    return m_pSingleton;  
}  
//END  
  
//Lazy man style: add lock, thread safety  
std::mutex mt;  
  
class Singleton  
{  
public:  
    static Singleton* getInstance();  
private:  
    Singleton(){}  
    Singleton(const Singleton&) = delete; //Explicitly reject  
    Singleton& operator=(const Singleton&) = delete; // explicitly reject  
  
    static Singleton* m_pSingleton;  
      
};  
Singleton* Singleton::m_pSingleton = NULL;  
  
Singleton* Singleton::getInstance()  
{  
    if(m_pSingleton == NULL)  
    {  
        mt.lock();  
        m_pSingleton = new Singleton();  
        mt.unlock();  
    }  
    return m_pSingleton;  
}  
//END  
  
//Return a reference to the local static object  
//Multithreading may be non-deterministic: any kind of non-const static object, whether it is local or non-local, will have trouble "waiting for something to happen" in a multithreaded environment. Solution: Manually call all reference-returning functions in the single-threaded startup phase of the program.  
class Singleton  
{  
public:  
    static Singleton& getInstance();  
private:  
    Singleton(){}  
    Singleton(const Singleton&) = delete; //Explicitly reject  
    Singleton& operator=(const Singleton&) = delete; // explicitly reject  
};  
  
  
Singleton& Singleton::getInstance()  
{  
    static Singleton singleton;  
    return singleton;  
}  
//END  
  
//Hungry Chinese style: thread safety, pay attention to delete  
class Singleton  
{  
public:  
    static Singleton* getInstance();  
private:  
    Singleton(){}  
    Singleton(const Singleton&) = delete; //Explicitly reject  
    Singleton& operator=(const Singleton&) = delete; // explicitly reject  
  
    static Singleton* m_pSingleton;  
};  
  
Singleton* Singleton::m_pSingleton = new Singleton();  
  
Singleton* Singleton::getInstance()  
{  
    return m_pSingleton;  
}  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324759812&siteId=291194637