设计模式-单例模式

1.6k words

说明

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

要点:

  1. 全局性:使用static关键字,变量需要存在于静态存储区
  2. 多线程:使用mutex进行保护
  3. 唯一性:构造函数私有,只生成一个实例,并且由自己创建

实现

单例类

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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的接口,有点麻烦,于是希望能用宏实现单例模式,最终使用效果为:

1
2
3
4
5
6
class Test
{
SINGELTON(Test)
private:
Test() {}
};

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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(); \
}

Comments