-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinary_Singleton.h
96 lines (82 loc) · 1.81 KB
/
Linary_Singleton.h
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*****************************************************************/
/* 设计模式————单例模式
/* 作者:李凝瑞
/* 时间:2015.05.29
/*****************************************************************/
#pragma once
#include "stdafx.h"
namespace linary_sigleton {
// 应用类
class App {
public:
virtual void Run() = 0;
};
// 微信类
class WeChat : public App {
public:
static WeChat * GetWeChatInstance() {
if (m_instance == NULL) {
m_instance = new WeChat();
}
return m_instance;
}
void Run() {
std::cout << "微信正在运行..." << std::endl;
}
protected:
WeChat() {}
private:
static WeChat * m_instance;
};
WeChat * WeChat::m_instance = NULL;
// 知乎类
class ZhiHu : public App {
public:
static ZhiHu * GetZhiHuInstance() {
if (m_instance == NULL) {
m_instance = new ZhiHu();
}
return m_instance;
}
void Run() {
std::cout << "知乎正在运行..." << std::endl;
}
protected:
ZhiHu() {}
private:
static ZhiHu * m_instance;
};
ZhiHu * ZhiHu::m_instance = NULL;
// 持有几个静态对象
class IPhone {
public:
IPhone() {
std::cout << "IPhone已解锁" << std::endl;
}
// 简单工厂模式
App * GetAppInstance(const std::string & app) {
if (app == "wechat") {
return WeChat::GetWeChatInstance();
} else if (app == "zhihu") {
return ZhiHu::GetZhiHuInstance();
} else {
return NULL;
}
}
};
// 测试代码
static void Test_Singleton() {
std::cout << "--------------------------" << std::endl;
std::cout << "-- 单例模式测试 --" << std::endl;
std::cout << "--------------------------" << std::endl;
IPhone * iPhone = new IPhone();
WeChat * wechat = WeChat::GetWeChatInstance();
wechat->Run();
ZhiHu * zhihu = ZhiHu::GetZhiHuInstance();
zhihu->Run();
delete zhihu;
delete wechat;
delete iPhone;
std::cout << std::endl;
}
};