Mike's blog
C++博客
::
首頁
::
聯系
::
聚合
::
管理
0 Posts :: 23 Stories :: 83 Comments :: 0 Trackbacks
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
(17)
給我留言
查看公開留言
查看私人留言
我參與的團隊
文章分類
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)
搜索
最新評論
1.?re: MYSQL外鍵(Foreign Key)的使用
通俗易懂
--666
2.?re: MYSQL外鍵(Foreign Key)的使用
很棒哦
--撒
3.?re: C和C++之間庫的互相調用
評論內容較長,點擊標題查看
--maxwell
4.?re: MYSQL外鍵(Foreign Key)的使用
this is good!
--wzz
5.?re: C和C++之間庫的互相調用
C++庫中含有類的情況希望能總結下哈,已轉載
--藝搜天下
常見設計模式之單件類(Singleton)
所謂的單件類就是保證一個類僅有一個實例,并提供一個訪問它的全局訪問點。
Singleton可以看作是一種經過改進的全局變量,既在一個進程中只能有唯一的實例,不允許產生第二個這樣的對象。
雖然單件類是最簡單的設計模式,但仍需小心使用,主要需注意:
1.構造函數
既然是只能有一個實例,那么構造函數自然不能被外部隨意調用,所以需要將其聲明為私有(private),包括默認構造、拷貝構造及賦值操作。至于是否需要實現要看具體應用。實例的產生需要一個輔助的成員函數(類似getInstance或creatInstance)。
2.析構函數
需要定義全局唯一的變量,我們首先會想到的就是靜態(static),沒錯,單件類也是通過靜態成員指針變量來實現單一。我們往往習慣于在析構函數中對成員指針進行內存釋放,但在單件類中是不可以這樣操作的,因為delete會調用類的析構,所以在自己的析構中delete自己的對象就會造成遞歸析構(無窮盡的析構現象)。
UML類圖:
實現代碼:
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
老狼
閱讀(1682)
評論(0)
編輯
收藏
引用
所屬分類:
C/C++
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
常見設計模式之抽象工廠模式
常見設計模式之代理模式(Proxy)
常見設計模式之工廠模式(Factory Method)
常見設計模式之簡單工廠模式(Static Factory Method)
常見設計模式之模板方法模式(Template Method)
常見設計模式之單件類(Singleton)
C和C++之間庫的互相調用
linux中的動態庫和靜態庫
C++的強制類型轉換
注意C++ STL容器中erase的使用
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Copyright @ 老狼
Powered by:
.Text
and
ASP.NET
Theme by:
.NET Monster
久久精品国产久精国产果冻传媒
|
久久国产色AV免费观看
|
青青久久精品国产免费看
|
久久久国产精品亚洲一区
|
久久精品人人做人人爽97
|
青青草原综合久久大伊人精品
|
国产免费久久精品99久久
|
久久婷婷五月综合色奶水99啪
|
国产精品一区二区久久精品
|
美女写真久久影院
|
久久综合久久综合亚洲
|
伊人久久综合热线大杳蕉下载
|
综合久久精品色
|
99久久精品费精品国产
|
亚洲国产精品久久久久婷婷软件
|
久久精品99无色码中文字幕
|
国内精品综合久久久40p
|
久久久久国产亚洲AV麻豆
|
久久这里只精品国产99热
|
狠狠色狠狠色综合久久
|
久久五月精品中文字幕
|
久久线看观看精品香蕉国产
|
久久亚洲精品成人AV
|
欧美亚洲国产精品久久高清
|
999久久久免费国产精品播放
|
色欲久久久天天天综合网
|
久久久久亚洲av毛片大
|
精品国产乱码久久久久久1区2区
|
亚洲综合熟女久久久30p
|
香蕉久久AⅤ一区二区三区
|
久久se精品一区二区影院
|
亚洲精品成人网久久久久久
|
99久久精品国产一区二区
|
国产69精品久久久久777
|
丰满少妇人妻久久久久久
|
国产亚洲精品美女久久久
|
日韩人妻无码精品久久久不卡
|
日韩人妻无码一区二区三区久久
|
国产一区二区精品久久岳
|
久久成人精品视频
|
品成人欧美大片久久国产欧美...
|