但行好事,莫问前程

单例模式

单例模式


单例模式的要点有三个:
1.单例类有且仅有一个实例
2.单例类必须自行创建自己的唯一实例
3.单例类必须给所有其他对象提供这一实例

#ifndef SINGLETON_
#define SINGLETON_H


class Singleton
{
public:
    static Singleton& GetInstace()
    {
        static Singleton instance;
        return instance;
    }
    void doSomething(){cout<<"do something\n";}
private:
    Singleton(){};
};

#endif // SINGLETON_H

由于会发生拷贝构造,自动生成

Singleton single = Singleton::GetInstance();

首先要解决的问题是阻止拷贝构造Singleton(const Singleton &)自动生成的东西 ,在进行值传递和对象赋值时会出现;

方法一:将GetInstance的返回值改为指针

static Singleton* GetInstace()
    {
        static Singleton instance;

        return &instance;
    }

方法二:声明拷贝函数,但是不进行定义,阻止自动拷贝构造。

#ifndef SINGLETON_H
#define SINGLETON_H
#include<iostream>
using std::cout;
class Singleton
{
public:
    static Singleton& GetInstace()
    {
        static Singleton instance;

        return instance;
    }
    void doSomething(){cout<<"do something\n";}
public:
     Singleton(){};
     Singleton(Singleton const &);
     Singleton& operator =(const Singleton &);
};
#endif // SINGLETON_H
#include <iostream>
#include"singleton.h"
using namespace std;
int main(int argc, char *argv[])
{
    Singleton::GetInstace().doSomething();
    return 0;

}

懒汉式/饿汉式

// singleton.h
#ifndef SINGLETON_H
#define SINGLETON_H

// 单例 - 懒汉式/饿汉式公用
class Singleton
{
public:
    static Singleton* GetInstance();

private:
    Singleton() {}  // 构造函数(被保护)

private:
    static Singleton *m_pSingleton;  // 指向单例对象的指针
};

#endif // SINGLETON_H

懒汉式的特点:

1.Lazy 初始化
2.非多线程安全
优点:第一次调用才初始化,避免内存浪费。
缺点:必须加锁(在“线程安全”部分分享如何加锁)才能保证单例,但加锁会影响效率。

// singleton.cpp
#include "singleton.h"

// 单例 - 懒汉式
Singleton *Singleton::m_pSingleton = NULL;

Singleton *Singleton::GetInstance()
{
    if (m_pSingleton == NULL)
        m_pSingleton = new Singleton();

    return m_pSingleton;
}

饿汉式的特点:

非 Lazy 初始化
多线程安全
优点:没有加锁,执行效率会提高。
缺点:类加载时就初始化,浪费内存。

// singleton.cpp
#include "singleton.h"

// 单例 - 饿汉式
Singleton *Singleton::m_pSingleton = new Singleton();

Singleton *Singleton::GetInstance()
{
    return m_pSingleton;
}

线程安全

// singleton.h
#ifndef SINGLETON_H
#define SINGLETON_H

#include <iostream>
#include <mutex>
using namespace std;

// 单例 - 懒汉式/饿汉式公用
class Singleton
{
public:
    static Singleton* GetInstance();

private:
    Singleton() {}  // 构造函数(被保护)

private:
    static Singleton *m_pSingleton;  // 指向单例对象的指针
    static mutex m_mutex;  // 锁
};

#endif // SINGLETON_H
// singleton.cpp
#include "singleton.h"

// 单例 - 懒汉式(双检锁 DCL 机制)
Singleton *Singleton::m_pSingleton = NULL;
mutex Singleton::m_mutex;

Singleton *Singleton::GetInstance()
{
    if (m_pSingleton == NULL) {
        std::lock_guard<std::mutex> lock(m_mutex);  // 自解锁
        if (m_pSingleton == NULL) {
            m_pSingleton = new Singleton();
        }
    }
    return m_pSingleton;
}

资源释放

// 单例 - 主动释放
static void DestoryInstance()
{
    if (m_pSingleton != NULL) {
        delete m_pSingleton;
        m_pSingleton = NULL;
    }
}
在析构函数释放就好
posted @ 2018-06-17 17:26  专业路过  阅读(114)  评论(0)    收藏  举报