蝸牛的家
男兒當自強
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
黑色天使
閱讀(599)
評論(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(轉)(6706)
2.?YUV格式詳細解釋與FFMPEG的關系(4321)
3.?如何檢測內存泄漏(轉)(3908)
4.?memcpy的BUG(2732)
5.?內存池技術學習筆記(2362)
評論排行榜
1.?CHttpDownLoad Beta 1.0(10)
2.?memcpy的BUG(5)
3.?事件模型SOCKET封裝(2)
4.?鍵盤過濾驅動源代碼(2)
5.?C++設計模式-Observer(1)
Copyright ©2025 黑色天使 Powered By
博客園
模板提供:
滬江博客
国产69精品久久久久777
|
久久这里只精品国产99热
|
久久亚洲视频
|
久久久久亚洲AV无码网站
|
91久久精一区二区三区大全
|
狠狠色丁香婷婷综合久久来来去
|
人妻无码久久一区二区三区免费
|
久久热这里只有精品在线观看
|
少妇久久久久久久久久
|
国产2021久久精品
|
精品国产青草久久久久福利
|
久久婷婷久久一区二区三区
|
久久久亚洲AV波多野结衣
|
亚洲成人精品久久
|
久久综合综合久久综合
|
天天做夜夜做久久做狠狠
|
国产精品久久久久影院嫩草
|
一本久久a久久精品综合香蕉
|
.精品久久久麻豆国产精品
|
四虎影视久久久免费观看
|
亚洲国产精品久久久久婷婷软件
|
99久久精品免费看国产一区二区三区
|
中文字幕久久精品无码
|
97精品伊人久久久大香线蕉
|
精品国产青草久久久久福利
|
久久91综合国产91久久精品
|
国内高清久久久久久
|
91精品国产色综久久
|
久久综合精品国产二区无码
|
亚洲午夜精品久久久久久app
|
久久电影网2021
|
久久久无码精品亚洲日韩按摩
|
合区精品久久久中文字幕一区
|
久久久青草青青亚洲国产免观
|
久久久久久久久波多野高潮
|
无码任你躁久久久久久
|
国产精品亚洲美女久久久
|
国产精品久久久久影院色
|
久久久久久久人妻无码中文字幕爆
|
久久精品国产福利国产琪琪
|
天天久久狠狠色综合
|