蝸牛的家
男兒當自強
C++博客
首頁
新文章
新隨筆
聚合
管理
posts - 48, comments - 21, trackbacks - 0
C++設計模式-visitor
意圖:
表示一個作用與某對象結構中的各元素的操作,它使你可以在不改變各元素的類的前提下定義作用于這些元素的新操作
UML圖:
適用:
一個對象結構包含很多類對象,他們有不同的接口,而你想對這些對象實施一些依賴于其具體類的操作
需要對一個對象結構中的對象進行很多不同的并且不相關的操作,而你想避免讓這些操作污染這些對象的類,Vi s i t o r 使得你可以將相關的操作集中起來定義在一個類中。當該對象結構被很多應用共享時,用Vi s i t o r 模式讓每個應用僅包含需要用到的操作
定義對象結構的類很少變化,但經常需要在此結構上定義新的操作,改變對象結構類需要重定義對所有訪問者的接口,這可能需要很大的代價,如果對象結構類經常改變,那么可能還有在這些類中定義這些操作較好
//
test.h
/**/
////////////////////////////////////////////////////////////////////////
//
class
Visitor;
class
Element
{
public
:
virtual
~
Element()
{}
virtual
void
Accept(Visitor
&
rVisitor)
=
0
;
protected
:
Element()
{}
}
;
class
ConCreateElementA :
public
Element
{
public
:
virtual
~
ConCreateElementA()
{}
virtual
void
Accept(Visitor
&
rVisitor);
}
;
class
ConCreateElementB :
public
Element
{
public
:
virtual
~
ConCreateElementB()
{}
virtual
void
Accept(Visitor
&
rVisitor);
}
;
class
Visitor
{
public
:
virtual
~
Visitor()
{}
virtual
void
VisitConcreateElementA(ConCreateElementA
*
pConcreateElementA)
=
0
;
virtual
void
VisitConcreateElementB(ConCreateElementB
*
pConcreateElementB)
=
0
;
protected
:
Visitor()
{}
}
;
class
ConcreateVisitorA
:
public
Visitor
{
public
:
virtual
~
ConcreateVisitorA()
{}
virtual
void
VisitConcreateElementA(ConCreateElementA
*
pConcreateElementA);
virtual
void
VisitConcreateElementB(ConCreateElementB
*
pConcreateElementB);
}
;
class
ConcreateVisitorB
:
public
Visitor
{
public
:
virtual
~
ConcreateVisitorB()
{}
virtual
void
VisitConcreateElementA(ConCreateElementA
*
pConcreateElementA);
virtual
void
VisitConcreateElementB(ConCreateElementB
*
pConcreateElementB);
}
;
//
test.cpp : Defines the entry point for the console application.
//
#include
"
stdafx.h
"
#include
<
iostream
>
#include
"
test.h
"
/**/
////////////////////////////////////////////////////////////////////////
//
void
ConCreateElementA::Accept(Visitor
&
rVisitor)
{
rVisitor.VisitConcreateElementA(
this
);
}
void
ConCreateElementB::Accept(Visitor
&
rVisitor)
{
rVisitor.VisitConcreateElementB(
this
);
}
void
ConcreateVisitorA::VisitConcreateElementA(ConCreateElementA
*
pConcreateElementA)
{
std::cout
<<
"
VisitConcreateElementA By ConcreateVisitorA\n
"
;
}
void
ConcreateVisitorA::VisitConcreateElementB(ConCreateElementB
*
pConcreateElementA)
{
std::cout
<<
"
VisitConcreateElementB By ConcreateVisitorA\n
"
;
}
void
ConcreateVisitorB::VisitConcreateElementA(ConCreateElementA
*
pConcreateElementA)
{
std::cout
<<
"
VisitConcreateElementA By ConcreateVisitorB\n
"
;
}
void
ConcreateVisitorB::VisitConcreateElementB(ConCreateElementB
*
pConcreateElementA)
{
std::cout
<<
"
VisitConcreateElementB By ConcreateVisitorB\n
"
;
}
/**/
////////////////////////////////////////////////////////////////////////
//
int
main(
int
argc,
char
*
argv[])
{
Visitor
*
pVisitor
=
new
ConcreateVisitorA;
Element
*
pElement
=
new
ConCreateElementA;
pElement
->
Accept(
*
pVisitor);
delete pElement;
delete pVisitor;
system(
"
pause
"
);
return
0
;
}
posted on 2008-08-23 12:18
黑色天使
閱讀(590)
評論(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(轉)(6689)
2.?YUV格式詳細解釋與FFMPEG的關系(4309)
3.?如何檢測內存泄漏(轉)(3902)
4.?memcpy的BUG(2713)
5.?內存池技術學習筆記(2351)
評論排行榜
1.?CHttpDownLoad Beta 1.0(10)
2.?memcpy的BUG(5)
3.?事件模型SOCKET封裝(2)
4.?鍵盤過濾驅動源代碼(2)
5.?C++設計模式-Observer(1)
Copyright ©2025 黑色天使 Powered By
博客園
模板提供:
滬江博客
色综合久久久久综合99
|
99久久免费国产精品
|
中文成人久久久久影院免费观看
|
欧美亚洲另类久久综合
|
久久精品亚洲福利
|
亚洲精品乱码久久久久66
|
久久99精品综合国产首页
|
亚洲精品久久久www
|
国产一久久香蕉国产线看观看
|
青青青青久久精品国产h久久精品五福影院1421
|
国产成人精品免费久久久久
|
一级做a爰片久久毛片16
|
久久精品国产亚洲av麻豆蜜芽
|
久久久精品人妻一区二区三区四
|
国产激情久久久久影院小草
|
亚洲AV无一区二区三区久久
|
久久99精品九九九久久婷婷
|
色综合久久久久无码专区
|
深夜久久AAAAA级毛片免费看
|
久久综合综合久久97色
|
亚洲AV无码久久精品蜜桃
|
久久亚洲精品无码播放
|
久久这里只精品国产99热
|
久久久精品人妻一区二区三区四
|
久久久久久午夜成人影院
|
无码任你躁久久久久久老妇App
|
久久久久久久99精品免费观看
|
久久精品国产清自在天天线
|
国产成人无码精品久久久免费
|
精品国产青草久久久久福利
|
麻豆久久久9性大片
|
香蕉久久影院
|
久久人人爽人人爽人人片AV东京热
|
国产精品99久久久久久人
|
久久久久无码精品国产
|
午夜精品久久久久久中宇
|
免费无码国产欧美久久18
|
伊人久久大香线蕉AV一区二区
|
久久精品综合网
|
久久人人爽人人爽人人av东京热
|
国产精品久久久天天影视
|