Home  >  Article  >  Backend Development  >  A brief introduction to singleton mode in C++ design patterns

A brief introduction to singleton mode in C++ design patterns

黄舟
黄舟Original
2017-01-18 15:07:331169browse

What is singleton pattern?

Guarantee that a class has only one instance and provide a global access point to it. [DP]

Let the class itself be responsible for saving its only instance. This class guarantees that no other instance can be created, and this class can provide a method to access the instance. 【DP】

When is singleton mode needed?

The program only needs one method to control a certain function and is not allowed to create a second function. For example: the number-taking machine used by banks.

To use singleton mode, you need to understand the usage of C++static keyword. This blog post of mine briefly analyzes static

Test case:

[code]int main(){
    //单例模式初始化两个实例的方法
    Singleton *s1 = Singleton::getInstance();
    Singleton *s2 = Singleton::getInstance();

    if(s1 == s2)
        std::cout << "Two the objects are the same instance.\n";
    else
        std::cout << "Two the objects are the different instance.\n";

    return 0;
}

Single case mode implementation:

[code]class Singleton{
private:
    //将构造函数声明为私有的,从而保证只允许类内使用
    Singleton(){}
    //声明一个类的静态对象(类外初始化)
    static Singleton *instance;
public:
    //静态成员方法,提供一个访问仅有实例的全局访问点。即提供接口创建对象
    static Singleton* getInstance(){
        if(instance != NULL){
            instance = new Singleton;
        }
        return instance;
    }
};
//类外部初始化静态成员变量(静态成员变量必须被初始化)
Singleton* Singleton::instance = NULL;

Attachment: If it is multi-threaded programming In this case, it is necessary to lock and judge whether it is empty twice.

[code]class Singleton{
private:
    //将构造函数声明为私有的,从而保证只允许类内使用
    Singleton(){}
    //声明一个类的静态对象(类外初始化)
    static Singleton *instance;
public:
    //静态成员方法,提供一个访问仅有实例的全局访问点。即提供接口创建对象
    static Singleton* getInstance(){

        if(instance != NULL){

            lock(syncObj){
               if(instance != NULL){
                   instance = new Singleton;
               }//if
            }//lock
        }//if
        return instance;
    }
};
//类外部初始化静态成员变量(静态成员变量必须被初始化)
Singleton* Singleton::instance = NULL;

The lock is added so that only one of the two threads can enter, while the other thread waits in line. After the first thread enters and comes out, the latter can enter. The second null judgment is to ensure that the first thread creates an instance, and the second thread will no longer create an instance after entering.

The above is the content of a brief understanding of the singleton mode in C++ design patterns. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn