[设计模式]单例模式案例之打印机案例

当我们启动某一个进程时,就会显示在任务管理器进程管理处;当我们关闭他后再次打开它时,发现启动进程和上次一模一样。也就是说我们启动时它只是在任务管理器上显示了出来,当我们关闭它后它实质上并没有终止,还在继续运行,只不过隐藏了起来。其实这就是单例设计模式。生活中有很多单例设计模式的案例,比如一个国家只有一个主席,即使主席替换,依然只有一个主席。比如某个教室只有一台打印机,那么无论多少人需要使用打印机,都将只能使用这一台打印机。也就是说该对象有且只能创建一个。

接下来我们总结一下单例模式的流程:

1.目的让类中只有一个对象,对象不需要自己释放。

2.将默认构造函数与拷贝构造函数私有化。

3.内部维护一个对象指针。

4.私有化唯一指针。

5.对外提供方法访问该指针。

6.保证了类中只能实例化唯一对象。

我们以打印机为例,某教室只有一台打印机,该打印机一次只能供一个人使用。

我们先来分析打印机,打印机具有打印功能,具有启动功能,具有关闭功能。而且具有一次只能供一人使用的特性,因此创建类:

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
class PrintingPress {
public:
static PrintingPress*getInstance() {
return unique;
}
bool check() {
return flag;
}
void openPrinting() {
if (flag == false) {
cout << "打印机已启动" << endl;
flag = true;
}
else {
cout << "打印机暂时被占用,无法打印" << endl;
}
}
void closePrinting() {
if (flag == true) {
flag = false;
cout << "打印机已关闭" << endl;
}
else {
cout << "打印机已关闭,无需执行该操作" << endl;
}
}
void usingPrinting(string str) {
if (flag == false) {
cout << "很抱歉,打印机被占用。" << endl;
}
else {
cout << str << endl;
}
}
private:
PrintingPress() {
cout << "打印机可正常使用" << endl;
flag = false;
}
PrintingPress(const PrintingPress& p) {
cout << "打印机违规使用" << endl;
flag = false;
}
static PrintingPress* unique;
bool flag = false;
};
PrintingPress* PrintingPress::unique = new PrintingPress;

用户具有使用打印机打印内容的功能,具有关闭打印机的功能,具有打开打印机的功能。因此创建用户类:

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
class user {
public:
void us() {
if (flag == true) {
string str;
cout << "请输入要打印的内容:" << endl;
cin >> str;
p->usingPrinting(str);
}
else {
cout << "打印机暂时被占用,无法打印" << endl;
}
}
void us(string str) {
if (flag == true) {
cout << "请输入要打印的内容:" << endl;
p->usingPrinting(str);
}
else {
cout << "打印机暂时被占用,无法打印" << endl;
}
}
void op() {
if (p->check() == false) {
p->openPrinting();
flag = true;
}
else {
cout << "打印机暂时被占用,无法打印" << endl;
}
}
void cl() {
if (flag = true) {
p->closePrinting();
flag = false;
}
else {
cout << "打印机暂时被占用,无法打印" << endl;
}
}
private:
PrintingPress* p = PrintingPress::getInstance();
bool flag = false;
};

以下代码是用C++写的一个简单的打印机案例测试代码。

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#define CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
class PrintingPress {
public:
static PrintingPress*getInstance() {
return unique;
}
bool check() {
return flag;
}
void openPrinting() {
if (flag == false) {
cout << "打印机已启动" << endl;
flag = true;
}
else {
cout << "打印机暂时被占用,无法打印" << endl;
}
}
void closePrinting() {
if (flag == true) {
flag = false;
cout << "打印机已关闭" << endl;
}
else {
cout << "打印机已关闭,无需执行该操作" << endl;
}
}
void usingPrinting(string str) {
if (flag == false) {
cout << "很抱歉,打印机被占用。" << endl;
}
else {
cout << str << endl;
}
}
private:
PrintingPress() {
cout << "打印机可正常使用" << endl;
flag = false;
}
PrintingPress(const PrintingPress& p) {
cout << "打印机违规使用" << endl;
flag = false;
}
static PrintingPress* unique;
bool flag = false;
};
PrintingPress* PrintingPress::unique = new PrintingPress;
class user {
public:
void us() {
if (flag == true) {
string str;
cout << "请输入要打印的内容:" << endl;
cin >> str;
p->usingPrinting(str);
}
else {
cout << "打印机暂时被占用,无法打印" << endl;
}
}
void us(string str) {
if (flag == true) {
cout << "请输入要打印的内容:" << endl;
p->usingPrinting(str);
}
else {
cout << "打印机暂时被占用,无法打印" << endl;
}
}
void op() {
if (p->check() == false) {
p->openPrinting();
flag = true;
}
else {
cout << "打印机暂时被占用,无法打印" << endl;
}
}
void cl() {
if (flag = true) {
p->closePrinting();
flag = false;
}
else {
cout << "打印机暂时被占用,无法打印" << endl;
}
}
private:
PrintingPress* p = PrintingPress::getInstance();
bool flag = false;
};
void test() {
user tmp[3];
tmp[1].op();
tmp[2].op();
tmp[1].us("lalalalala");
tmp[2].op();
tmp[1].cl();
tmp[2].op();
tmp[2].us("22222222");
tmp[2].cl();
}
int main() {
test();
system("pause");
return 0;
}

文章结束了,但我们的故事还在继续
坚持原创技术分享,您的支持将鼓励我继续创作!