Mike's blog
C++博客
::
首頁(yè)
::
聯(lián)系
::
聚合
::
管理
0 Posts :: 23 Stories :: 83 Comments :: 0 Trackbacks
常用鏈接
我的隨筆
我的評(píng)論
我參與的隨筆
留言簿
(17)
給我留言
查看公開留言
查看私人留言
我參與的團(tuán)隊(duì)
文章分類
C/C++(18)
Database(14)
LINUX(2)
OS(1)
Other(1)
文章檔案
2010年8月 (5)
2010年7月 (2)
2009年5月 (1)
2009年3月 (2)
2009年2月 (1)
2009年1月 (1)
2008年12月 (3)
2008年11月 (4)
2008年10月 (3)
2008年2月 (1)
搜索
最新評(píng)論
1.?re: MYSQL外鍵(Foreign Key)的使用
通俗易懂
--666
2.?re: MYSQL外鍵(Foreign Key)的使用
很棒哦
--撒
3.?re: C和C++之間庫(kù)的互相調(diào)用
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--maxwell
4.?re: MYSQL外鍵(Foreign Key)的使用
this is good!
--wzz
5.?re: C和C++之間庫(kù)的互相調(diào)用
C++庫(kù)中含有類的情況希望能總結(jié)下哈,已轉(zhuǎn)載
--藝搜天下
常見設(shè)計(jì)模式之單件類(Singleton)
所謂的單件類就是保證一個(gè)類僅有一個(gè)實(shí)例,并提供一個(gè)訪問(wèn)它的全局訪問(wèn)點(diǎn)。
Singleton可以看作是一種經(jīng)過(guò)改進(jìn)的全局變量,既在一個(gè)進(jìn)程中只能有唯一的實(shí)例,不允許產(chǎn)生第二個(gè)這樣的對(duì)象。
雖然單件類是最簡(jiǎn)單的設(shè)計(jì)模式,但仍需小心使用,主要需注意:
1.構(gòu)造函數(shù)
既然是只能有一個(gè)實(shí)例,那么構(gòu)造函數(shù)自然不能被外部隨意調(diào)用,所以需要將其聲明為私有(private),包括默認(rèn)構(gòu)造、拷貝構(gòu)造及賦值操作。至于是否需要實(shí)現(xiàn)要看具體應(yīng)用。實(shí)例的產(chǎn)生需要一個(gè)輔助的成員函數(shù)(類似getInstance或creatInstance)。
2.析構(gòu)函數(shù)
需要定義全局唯一的變量,我們首先會(huì)想到的就是靜態(tài)(static),沒(méi)錯(cuò),單件類也是通過(guò)靜態(tài)成員指針變量來(lái)實(shí)現(xiàn)單一。我們往往習(xí)慣于在析構(gòu)函數(shù)中對(duì)成員指針進(jìn)行內(nèi)存釋放,但在單件類中是不可以這樣操作的,因?yàn)閐elete會(huì)調(diào)用類的析構(gòu),所以在自己的析構(gòu)中delete自己的對(duì)象就會(huì)造成遞歸析構(gòu)(無(wú)窮盡的析構(gòu)現(xiàn)象)。
UML類圖:
實(shí)現(xiàn)代碼:
1)Singleton.hpp
1
/**/
/*
*******************************************************************
2
* Copyright (c) 2010~2010 All Rights Resverved by wei.chen.
3
*******************************************************************
*/
4
/**/
/*
*
5
* @file Singleton.hpp
6
* @brief Declare the class of Singleton.
7
* @version 0.1
8
* @since 0.1
9
* @author chenwei<76487974@qq.com>
10
* @date 2010-7-19 Created it
11
*/
12
13
#ifndef _SINGLETON_HPP
14
#define
_SINGLETON_HPP
15
16
#include
<
iostream
>
17
18
class
Singleton
19
{
20
public
:
21
~
Singleton()
{
22
std::cout
<<
"
Singleton destructor.
"
<<
std::endl;
23
}
24
25
static
Singleton
*
creatInstance();
26
static
void
destroyInstance();
27
void
test()
{
28
std::cout
<<
"
Singleton test.
"
<<
std::endl;
29
}
30
31
private
:
32
static
Singleton
*
m_pInstance;
33
34
Singleton()
{
35
std::cout
<<
"
Singleton constructor.
"
<<
std::endl;
36
}
37
38
Singleton(Singleton
&
);
39
Singleton
&
operator
=
(Singleton
&
);
40
}
;
41
42
#endif
43
44
2)Singleton.cpp
1
/**/
/*
*******************************************************************
2
* Copyright (c) 2010~2010 All Rights Resverved by wei.chen.
3
*******************************************************************
*/
4
/**/
/*
*
5
* @file Singleton.cpp
6
* @brief Implement the methods of the class Singleton.
7
* @version 0.1
8
* @since 0.1
9
* @author chenwei<76487974@qq.com>
10
* @date 2010-7-19 Created it
11
*/
12
13
#include
"
Singleton.hpp
"
14
#include
<
stdlib.h
>
15
16
Singleton
*
Singleton::m_pInstance
=
NULL;
17
18
/**/
/*
*
19
* @fn creatInstance
20
* @brief Create a Singleton instance.
21
* @return A pointer to Singleton Instance, or NULL if failed.
22
* @author wei.chen (2010-7-19)
23
*/
24
Singleton
*
Singleton::creatInstance()
25
{
26
std::cout
<<
"
Create the instance.
"
<<
std::endl;
27
if
(
!
m_pInstance)
{
28
m_pInstance
=
new
Singleton();
29
if
(
!
m_pInstance)
{
30
std::cout
<<
"
No memory to new for Singleton.
"
<<
std::endl;
31
abort();
32
}
33
}
34
35
return
m_pInstance;
36
}
37
38
/**/
/*
*
39
* @fn destroyInstance
40
* @brief Release the memory for destroying the instance.
41
* @author wei.chen (2010-7-19)
42
*/
43
void
Singleton::destroyInstance()
44
{
45
std::cout
<<
"
Destroy the instance.
"
<<
std::endl;
46
delete m_pInstance;
47
m_pInstance
=
NULL;
48
}
49
3)Main.cpp
1
/**/
/*
*******************************************************************
2
* Copyright (c) 2010~2010 All Rights Resverved by wei.chen.
3
*******************************************************************
*/
4
/**/
/*
*
5
* @file Main.cpp
6
* @brief The entrance of the program.
7
* @version 0.1
8
* @since 0.1
9
* @author chenwei<76487974@qq.com>
10
* @date 2010-7-19 Created it
11
*/
12
13
#include
"
Singleton.hpp
"
14
15
/**/
/*
*
16
* @fn main
17
* @brief The entrance of the program.
18
* @return int
19
* @retval 0-normal
20
* @author wei.chen (2010-7-19)
21
*/
22
int
main()
23
{
24
Singleton
*
singletonTest
=
Singleton::creatInstance();
25
if
(
!
singletonTest)
{
26
std::cout
<<
"
Create Instance failed.
"
<<
std::endl;
27
return
-
1
;
28
}
29
30
singletonTest
->
test();
31
Singleton::destroyInstance();
32
33
return
0
;
34
}
35
posted on 2010-07-19 23:39
老狼
閱讀(1688)
評(píng)論(0)
編輯
收藏
引用
所屬分類:
C/C++
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
【推薦】100%開源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
常見設(shè)計(jì)模式之抽象工廠模式
常見設(shè)計(jì)模式之代理模式(Proxy)
常見設(shè)計(jì)模式之工廠模式(Factory Method)
常見設(shè)計(jì)模式之簡(jiǎn)單工廠模式(Static Factory Method)
常見設(shè)計(jì)模式之模板方法模式(Template Method)
常見設(shè)計(jì)模式之單件類(Singleton)
C和C++之間庫(kù)的互相調(diào)用
linux中的動(dòng)態(tài)庫(kù)和靜態(tài)庫(kù)
C++的強(qiáng)制類型轉(zhuǎn)換
注意C++ STL容器中erase的使用
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問(wèn)
Chat2DB
管理
Copyright @ 老狼
Powered by:
.Text
and
ASP.NET
Theme by:
.NET Monster
91久久精品国产91性色也
|
久久久久亚洲AV成人网人人网站
|
国产精品久久久久久五月尺
|
无码人妻少妇久久中文字幕
|
一级a性色生活片久久无少妇一级婬片免费放
|
精品无码久久久久国产动漫3d
|
人妻无码精品久久亚瑟影视
|
久久久久久久亚洲Av无码
|
久久国产亚洲精品麻豆
|
Xx性欧美肥妇精品久久久久久
|
久久九九久精品国产免费直播
|
77777亚洲午夜久久多喷
|
久久亚洲电影
|
久久国产精品久久国产精品
|
亚洲欧洲久久久精品
|
91精品国产乱码久久久久久
|
99久久99久久精品国产片
|
亚洲欧洲精品成人久久曰影片
|
久久精品亚洲一区二区三区浴池
|
热久久这里只有精品
|
久久综合给合久久狠狠狠97色69
|
精品国产青草久久久久福利
|
久久水蜜桃亚洲av无码精品麻豆
|
无码国内精品久久人妻蜜桃
|
国产ww久久久久久久久久
|
99精品久久精品一区二区
|
久久久久亚洲AV综合波多野结衣
|
性做久久久久久久
|
久久中文字幕视频、最近更新
|
久久99精品综合国产首页
|
色婷婷久久综合中文久久蜜桃av
|
少妇久久久久久被弄到高潮
|
久久国产免费直播
|
久久精品中文字幕一区
|
国产精品久久久久久久久久免费
|
久久99国产精品99久久
|
精品少妇人妻av无码久久
|
久久婷婷五月综合色高清
|
国产∨亚洲V天堂无码久久久
|
色偷偷偷久久伊人大杳蕉
|
久久亚洲春色中文字幕久久久
|