Loading

设计模式-单例模式

说明

目的:为了保证一个类在程序中只有一个实例,并且能被全局访问

场景:全局线程池

要点:

  1. 全局性:使用static关键字,变量需要存在于静态存储区
  2. 多线程:使用mutex进行保护
  3. 唯一性:只生成一个实例

实现

单例类

通过 Test::Instance()获取类指针

class Test
{
public:
    static Test* Instance()
    {
        if (m_instance == nullptr)
        {
            std::lock_guard<std::mutex> lock(m_mutex);
            if (m_instance == nullptr)
                m_instance = new Test;
        }
        return m_instance;
    }

    static void DeleteInstance()
    {
        if (m_instance)
        {
            std::lock_guard<std::mutex> lock(m_mutex);
            if (m_instance)
            {
                delete m_instance;
                m_instance = nullptr;
            }
        }
    }

private:
    Test() {}

private:
    static Test *m_instance;
    static std::mutex m_mutex;
};

Test* Test::m_instance = NULL;
std::mutex Test::m_mutex;

单例宏

每个类想要实现单例模式,都要写一遍Instance的接口,有点麻烦,于是希望能用宏实现单例模式,最终使用效果为:

class Test
{
    SINGELTON(Test)
private:
    Test() {}
};

可以用类模板的方式来进行:

template<class T>
class Singleton
{

public:
    static T *Instance()
    {
        if (m_instance == nullptr)
        {
            std::lock_guard<std::mutex> lock(m_instanceMutex);
            if (m_instance == nullptr)
                m_instance = std::unique_ptr<T>(new T);
        }
        return m_instance.get();
    }

private:
    static std::unique_ptr<T> m_instance;
    static std::mutex m_instanceMutex;
};

template<class T> std::mutex Singleton<T>::m_instanceMutex;
template<class T> std::unique_ptr<T> Singleton<T>::m_instance(nullptr);

//单例宏
#define SINGELTON(OBJ_CLASS)                            \
    friend Singleton<OBJ_CLASS>;                        \
                                                        \
public:                                                 \
    static OBJ_CLASS *Instance()                        \
    {                                                   \
        return Singleton<OBJ_CLASS>::Instance();        \
    }

posted @ 2022-06-07 15:28  Yaronzz  阅读(43)  评论(0编辑  收藏  举报