蝸牛的家
男兒當自強
C++博客
首頁
新文章
新隨筆
聚合
管理
posts - 48, comments - 21, trackbacks - 0
C++模式-Iterator
意圖:
提供一種方法順序訪問一個聚合對象中各個元素,而又不暴露該對象的內部表示
UML圖:
適用:
訪問一個聚合對象的內容而無需暴露它的內部表示
支持對聚合對象的多種遍歷
為遍歷不同的聚合結構提供一個統一的接口
//
test.h
typedef
int
DATA;
/**/
////////////////////////////////////////////////////////////////////////
//
class
Iterater;
class
Aggregate
{
public
:
virtual
~
Aggregate()
{}
virtual
int
GetSize()
=
0
;
virtual
DATA GetItem(
int
nIndex)
=
0
;
}
;
class
Iterater
{
public
:
virtual
~
Iterater()
{}
virtual
void
First()
=
0
;
virtual
void
Next()
=
0
;
virtual
bool
IsDone()
=
0
;
virtual
DATA CurrentIter()
=
0
;
protected
:
Aggregate
*
m_pConCreateAggregate;
int
m_nIndex;
}
;
class
ConCreateAggregate :
public
Aggregate
{
public
:
ConCreateAggregate(
int
nSize);
virtual
~
ConCreateAggregate();
virtual
int
GetSize();
virtual
DATA GetItem(
int
nIndex);
private
:
int
m_nSize;
DATA
*
m_pData;
}
;
class
ConCreateIterater :
public
Iterater
{
public
:
ConCreateIterater(Aggregate
*
pAggregate);
virtual
~
ConCreateIterater()
{}
virtual
void
First();
virtual
void
Next();
virtual
bool
IsDone();
virtual
DATA CurrentIter();
}
;
//
test.cpp : Defines the entry point for the console application.
//
#include
"
stdafx.h
"
#include
<
iostream
>
#include
"
test.h
"
/**/
////////////////////////////////////////////////////////////////////////
//
ConCreateAggregate::ConCreateAggregate(
int
nSize) : m_nSize(nSize),m_pData(NULL)
{
m_pData
=
new
DATA[m_nSize];
for
(
int
i
=
0
; i
<
nSize;
++
i)
{
m_pData[i]
=
i;
}
}
ConCreateAggregate::
~
ConCreateAggregate()
{
delete []m_pData;
m_pData
=
NULL;
}
int
ConCreateAggregate::GetSize()
{
return
m_nSize;
}
DATA ConCreateAggregate::GetItem(
int
nIndex)
{
//
對外提供相同的接口,得到特定次序的值
if
(nIndex
<
m_nSize)
{
return
m_pData[nIndex];
}
else
{
return
-
1
;
}
}
ConCreateIterater::ConCreateIterater(Aggregate
*
pAggregate)
{
m_pConCreateAggregate
=
pAggregate;
m_nIndex
=
0
;
}
void
ConCreateIterater::First()
{
m_nIndex
=
0
;
}
void
ConCreateIterater::Next()
{
if
(m_nIndex
<
m_pConCreateAggregate
->
GetSize())
{
++
m_nIndex;
}
}
bool
ConCreateIterater::IsDone()
{
return
m_nIndex
==
m_pConCreateAggregate
->
GetSize();
}
DATA ConCreateIterater::CurrentIter()
{
//
間接引用此函數得到值
return
m_pConCreateAggregate
->
GetItem(m_nIndex);
}
/**/
////////////////////////////////////////////////////////////////////////
//
int
main(
int
argc,
char
*
argv[])
{
Aggregate
*
pAggregate
=
new
ConCreateAggregate(
4
);
Iterater
*
pIterater
=
new
ConCreateIterater(pAggregate);
for
(;
false
==
pIterater
->
IsDone(); pIterater
->
Next())
{
std::cout
<<
pIterater
->
CurrentIter()
<<
"
\n
"
;
}
system(
"
pause
"
);
return
0
;
}
posted on 2008-08-23 01:18
黑色天使
閱讀(579)
評論(0)
編輯
收藏
引用
所屬分類:
設計模式
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
decorator模式
MVC模式理解——當年給我一個browser多好(轉)
C++設計模式-趣解
C++設計模式-visitor
C++設計模式-Memento
C++模式-Iterator
C++設計模式-Observer
C++設計模式-Command
C++模式-FlyWeight
C++設計模式-ChainOfResponsibility
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
<
2008年8月
>
日
一
二
三
四
五
六
27
28
29
30
31
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
1
2
3
4
5
6
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
(2)
給我留言
查看公開留言
查看私人留言
隨筆分類
C\C++(8)
Hacker(1)
STL
VC&MFC(4)
操作系統(1)
多進程&多線程
流媒體開發
內存管理技術(2)
軟件工程(1)
設計模式(20)
數據結構&算法(2)
網絡開發(3)
隨筆檔案
2011年4月 (1)
2011年3月 (2)
2009年7月 (1)
2009年6月 (2)
2009年3月 (1)
2009年2月 (3)
2009年1月 (3)
2008年12月 (5)
2008年11月 (1)
2008年10月 (3)
2008年9月 (3)
2008年8月 (23)
文章檔案
2011年3月 (1)
2009年6月 (1)
2008年11月 (1)
搜索
最新評論
1.?re: C++設計模式-Observer
評論內容較長,點擊標題查看
--no7dw
2.?re: YUV格式詳細解釋與FFMPEG的關系
評論內容較長,點擊標題查看
--windsome
3.?re: 鍵盤過濾驅動源代碼
@soul
再怎么懶也應該自己實現一部分吧
--黑色天使
4.?re: 鍵盤過濾驅動源代碼[未登錄]
再怎么懶也該加上unload例程吧
--soul
5.?re: CHttpDownLoad Beta 1.0
評論內容較長,點擊標題查看
--tangxinfa
閱讀排行榜
1.?RGB、YUY2、YUYV、YVYU、UYVY與AYUV(轉)(6722)
2.?YUV格式詳細解釋與FFMPEG的關系(4333)
3.?如何檢測內存泄漏(轉)(3911)
4.?memcpy的BUG(2741)
5.?內存池技術學習筆記(2372)
評論排行榜
1.?CHttpDownLoad Beta 1.0(10)
2.?memcpy的BUG(5)
3.?事件模型SOCKET封裝(2)
4.?鍵盤過濾驅動源代碼(2)
5.?C++設計模式-Observer(1)
Copyright ©2025 黑色天使 Powered By
博客園
模板提供:
滬江博客
99久久精品国产毛片
|
久久久精品久久久久影院
|
国产精品久久久久久
|
久久综合精品国产一区二区三区
|
午夜精品久久久久久影视riav
|
精品永久久福利一区二区
|
久久精品成人
|
久久国产精品99精品国产987
|
久久精品无码专区免费
|
亚洲国产成人久久精品动漫
|
久久免费99精品国产自在现线
|
99久久国产精品免费一区二区
|
99久久国产热无码精品免费久久久久
|
99久久做夜夜爱天天做精品
|
女人香蕉久久**毛片精品
|
久久久久亚洲av成人网人人软件
|
久久99热只有频精品8
|
尹人香蕉久久99天天拍
|
亚洲午夜精品久久久久久人妖
|
久久人人爽人人爽人人AV东京热
|
亚洲精品99久久久久中文字幕
|
色偷偷888欧美精品久久久
|
欧美熟妇另类久久久久久不卡
|
武侠古典久久婷婷狼人伊人
|
国产91久久综合
|
99久久精品国产毛片
|
国产精品成人精品久久久
|
青青国产成人久久91网
|
91久久精品视频
|
国产三级精品久久
|
久久五月精品中文字幕
|
久久天天躁狠狠躁夜夜不卡
|
99久久精品国产毛片
|
久久se精品一区二区影院
|
国产高潮久久免费观看
|
中文字幕一区二区三区久久网站
|
青青国产成人久久91网
|
人妻中文久久久久
|
久久久久久久精品妇女99
|
久久久噜噜噜久久中文字幕色伊伊
|
久久丝袜精品中文字幕
|