蝸牛的家
男兒當自強
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
博客園
模板提供:
滬江博客
欧美粉嫩小泬久久久久久久
|
99久久精品这里只有精品
|
亚洲乱码中文字幕久久孕妇黑人
|
久久精品国产91久久综合麻豆自制
|
久久99热只有频精品8
|
久久国产精品久久久
|
国产高清国内精品福利99久久
|
日韩十八禁一区二区久久
|
久久久久成人精品无码中文字幕
|
99久久免费国产精品
|
波多野结衣久久一区二区
|
国产精品99久久久久久www
|
久久综合久久综合亚洲
|
久久一区二区三区免费
|
久久综合精品国产二区无码
|
中文字幕久久精品无码
|
思思久久精品在热线热
|
91精品国产91热久久久久福利
|
2021国产精品午夜久久
|
狠狠人妻久久久久久综合
|
国产精品伦理久久久久久
|
伊人久久综合成人网
|
久久久久人妻精品一区三寸蜜桃
|
久久国产午夜精品一区二区三区
|
伊人久久大香线蕉综合Av
|
久久久国产精华液
|
国产亚洲色婷婷久久99精品91
|
久久青青草原亚洲av无码app
|
欧美亚洲另类久久综合婷婷
|
久久精品国产精品国产精品污
|
少妇久久久久久久久久
|
久久亚洲AV成人无码软件
|
尹人香蕉久久99天天拍
|
久久久无码精品亚洲日韩京东传媒
|
久久精品国产亚洲5555
|
9久久9久久精品
|
人人狠狠综合久久亚洲88
|
国产精品久久网
|
国产91色综合久久免费
|
久久久无码一区二区三区
|
日韩人妻无码精品久久久不卡
|