蝸牛的家
男兒當(dāng)自強(qiáng)
C++博客
首頁(yè)
新文章
新隨筆
聚合
管理
posts - 48, comments - 21, trackbacks - 0
C++設(shè)計(jì)模式-visitor
意圖:
表示一個(gè)作用與某對(duì)象結(jié)構(gòu)中的各元素的操作,它使你可以在不改變各元素的類的前提下定義作用于這些元素的新操作
UML圖:
適用:
一個(gè)對(duì)象結(jié)構(gòu)包含很多類對(duì)象,他們有不同的接口,而你想對(duì)這些對(duì)象實(shí)施一些依賴于其具體類的操作
需要對(duì)一個(gè)對(duì)象結(jié)構(gòu)中的對(duì)象進(jìn)行很多不同的并且不相關(guān)的操作,而你想避免讓這些操作污染這些對(duì)象的類,Vi s i t o r 使得你可以將相關(guān)的操作集中起來(lái)定義在一個(gè)類中。當(dāng)該對(duì)象結(jié)構(gòu)被很多應(yīng)用共享時(shí),用Vi s i t o r 模式讓每個(gè)應(yīng)用僅包含需要用到的操作
定義對(duì)象結(jié)構(gòu)的類很少變化,但經(jīng)常需要在此結(jié)構(gòu)上定義新的操作,改變對(duì)象結(jié)構(gòu)類需要重定義對(duì)所有訪問者的接口,這可能需要很大的代價(jià),如果對(duì)象結(jié)構(gòu)類經(jīng)常改變,那么可能還有在這些類中定義這些操作較好
//
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)
評(píng)論(0)
編輯
收藏
引用
所屬分類:
設(shè)計(jì)模式
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
【推薦】100%開源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
decorator模式
MVC模式理解——當(dāng)年給我一個(gè)browser多好(轉(zhuǎn))
C++設(shè)計(jì)模式-趣解
C++設(shè)計(jì)模式-visitor
C++設(shè)計(jì)模式-Memento
C++模式-Iterator
C++設(shè)計(jì)模式-Observer
C++設(shè)計(jì)模式-Command
C++模式-FlyWeight
C++設(shè)計(jì)模式-ChainOfResponsibility
網(wǎng)站導(dǎo)航:
博客園
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
常用鏈接
我的隨筆
我的評(píng)論
我參與的隨筆
留言簿
(2)
給我留言
查看公開留言
查看私人留言
隨筆分類
C\C++(8)
Hacker(1)
STL
VC&MFC(4)
操作系統(tǒng)(1)
多進(jìn)程&多線程
流媒體開發(fā)
內(nèi)存管理技術(shù)(2)
軟件工程(1)
設(shè)計(jì)模式(20)
數(shù)據(jù)結(jié)構(gòu)&算法(2)
網(wǎng)絡(luò)開發(fā)(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)
搜索
最新評(píng)論
1.?re: C++設(shè)計(jì)模式-Observer
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--no7dw
2.?re: YUV格式詳細(xì)解釋與FFMPEG的關(guān)系
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--windsome
3.?re: 鍵盤過濾驅(qū)動(dòng)源代碼
@soul
再怎么懶也應(yīng)該自己實(shí)現(xiàn)一部分吧
--黑色天使
4.?re: 鍵盤過濾驅(qū)動(dòng)源代碼[未登錄]
再怎么懶也該加上unload例程吧
--soul
5.?re: CHttpDownLoad Beta 1.0
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--tangxinfa
閱讀排行榜
1.?RGB、YUY2、YUYV、YVYU、UYVY與AYUV(轉(zhuǎn))(6689)
2.?YUV格式詳細(xì)解釋與FFMPEG的關(guān)系(4310)
3.?如何檢測(cè)內(nèi)存泄漏(轉(zhuǎn))(3902)
4.?memcpy的BUG(2714)
5.?內(nèi)存池技術(shù)學(xué)習(xí)筆記(2351)
評(píng)論排行榜
1.?CHttpDownLoad Beta 1.0(10)
2.?memcpy的BUG(5)
3.?事件模型SOCKET封裝(2)
4.?鍵盤過濾驅(qū)動(dòng)源代碼(2)
5.?C++設(shè)計(jì)模式-Observer(1)
Copyright ©2025 黑色天使 Powered By
博客園
模板提供:
滬江博客
久久精品国产亚洲AV高清热
|
久久精品人成免费
|
精品人妻伦一二三区久久
|
久久久久亚洲精品中文字幕
|
欧美亚洲国产精品久久
|
亚洲国产欧洲综合997久久
|
国产精品久久影院
|
色天使久久综合网天天
|
精品国产乱码久久久久久呢
|
久久99精品国产麻豆宅宅
|
久久精品国产一区二区
|
一本色道久久综合亚洲精品
|
7777久久亚洲中文字幕
|
亚洲精品无码久久毛片
|
精品一区二区久久
|
精品久久久无码21p发布
|
国产ww久久久久久久久久
|
伊人久久久AV老熟妇色
|
久久露脸国产精品
|
女人香蕉久久**毛片精品
|
久久99久久99精品免视看动漫
|
亚洲Av无码国产情品久久
|
久久99亚洲网美利坚合众国
|
亚洲综合久久夜AV
|
91超碰碰碰碰久久久久久综合
|
色综合久久久久久久久五月
|
中文字幕无码久久人妻
|
国内精品久久久久久久影视麻豆
|
伊人久久大香线蕉综合影院首页
|
久久精品一区二区影院
|
99精品久久久久久久婷婷
|
久久久精品国产sm调教网站
|
无码日韩人妻精品久久蜜桃
|
久久久久亚洲国产
|
精品久久久久久久国产潘金莲
|
精品国产一区二区三区久久
|
久久精品国产亚洲AV嫖农村妇女
|
亚洲国产精品无码久久久秋霞2
|
亚洲AV无码久久精品色欲
|
亚洲精品乱码久久久久久蜜桃不卡
|
久久精品国产男包
|