2014-01-18 星期六 11:12:42
Singleton,继续GOF。
1、Intent
Ensure a class only has one instance, and provide a global point of access to it.
保证一个类仅有一个实例,并提供一个访问它的全局访问点。
2、Also Known As
3、Motivation
?It‘s important for some classes to have exactly one instance. Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system and one window manager. A digital filter will have one A/D converter. An accounting system will be dedicated to serving one company.
对一些类来说,只有一个实例是很重要的。虽然系统中可以有许多打印机,但却只应该有一个打印假脱机( printer spooler) ,只应该有一个文件系统和一个窗口管理器。一个数字滤波器只能有一个A / D转换器。一个会计系统只能专用于一个公司。
How do we ensure that a class has only one instance and that the instance is easily accessible? A global variable makes an object accessible, but it doesn‘t keep you from instantiating multiple objects.
我们怎么样才能保证一个类只有一个实例并且这个实例易于被访问呢?一个全局变量使得一个对象可以被访问,但它不能防止你实例化多个对象。
?A better solution is to make the class itself responsible for keeping track of its sole instance. The class can ensure that no other instance can be created (by intercepting requests to create new objects), and it can provide a way to access the instance. This is the Singleton pattern.
一个更好的办法是,让类自身负责保存它的唯一实例。这个类可以保证没有其他实例可以被创建(通过截取创建新对象的请求) ,并且它可以提供一个访问该实例的方法。
这就是Singleton模式。
4、Applicability
Use the Singleton pattern when
● there must be exactly one instance of a class, and it must be accessible to clients from a wellknown access point.
?● when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.
在下面的情况下可以使用Singleton模式
● 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。
● 当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。
5、Structure
?
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
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
112
113
114
115
|
#include <iostream> #include <memory> #include <cstring> #include <unistd.h> #define STATIC_IN_CLASS //实际应用中可以宏定义 ///////////////// 单件模式 #define PATTERN_SINGLETON_DECLARE(classname) \ static
classname * instance();
#define PATTERN_SINGLETON_IMPLEMENT(classname) \ classname * classname::instance() \ { \ static
classname * _instance = NULL; \
if ( NULL == _instance) \
{ \
_instance = new
classname; \
} \
return
_instance; \
} class
CSingleton
{ public :
PATTERN_SINGLETON_DECLARE(CSingleton);
protected :
CSingleton() //not public
{
std::cout<< "CSingleton::CSingleton()" <<std::endl;
}
public :
~CSingleton(){};
public :
void
is_singel()
{
std::cout<< "is_singel(),m_test = " <<m_test<<std::endl;
}
CSingleton& operator=( const
CSingleton& rhs)
{
if ( this
!= &rhs)
{
m_test = rhs.m_test;
}
std::cout<< "operator=" <<std::endl;
return
* this ;
}
public :
int
m_test;
static
CSingleton* _instance; //按gof类图放在里面
static
const
int
t = 0;
/*静态数据在c++中只有 static const int t = 0;
可以初始化。
其他都要在类的外面。
*/
}; //PATTERN_SINGLETON_IMPLEMENT(CSingleton); //ok CSingleton* CSingleton::_instance = NULL; CSingleton * CSingleton::instance() { if ( NULL == _instance)
{
_instance = new
CSingleton;
}
else
{
//exception
}
return
_instance;
} #define g_CSingleton (*CSingleton::instance()) #define g_pCSingleton (CSingleton::instance()) int
main( int
argc, char
*argv[])
{ //只有一次输出"CSingleton::CSingleton()"
CSingleton *ps1 = CSingleton::instance();
ps1->is_singel();
CSingleton *ps2 = g_pCSingleton;
ps2->is_singel();
CSingleton s2 = *CSingleton::instance();
s2.is_singel();
CSingleton s21 = g_CSingleton;
s21.is_singel();
sleep(1);
(*CSingleton::instance()).is_singel();
std::cout << std::endl << "This is a Singelton Antother" <<std::endl;
std::cout << "Not Get By instance()" <<std::endl;
#if 0 //public的话,可以编译通过
CSingleton s3;
s3.m_test = 3;
CSingleton s4=s3;
s4.is_singel();
s4 = *CSingleton::instance();
s4.is_singel();
#endif
return
0;
} |