那誰的技術博客
感興趣領域:高性能服務器編程,存儲,算法,Linux內核
隨筆 - 210, 文章 - 0, 評論 - 1183, 引用 - 0
數據加載中……
常見設計模式的解析和實現(C++)之十一-TemplateMethod模式
作用:
定義一個操作中的算法的骨架,而將一些步驟延遲到子類中。TemplateMethod 使得子類可以不改變一個算法的結構即可重定義該算法的某些特定步驟。
UML結構圖:
抽象基類:
1)AbstractClass:抽象基類,定義算法的輪廓
解析:
TemplateMethod 的關鍵在于在基類中定義了一個算法的輪廓,但是算法每一步具體的實現留給了派生類.但是這樣也會造成設計的靈活性不高的缺點,因為輪廓已經定下來了要想改變就比較難了,這也是為什么優先采用聚合而不是繼承的原因.
實現:
1)TemplateMethod.h
/**/
/*
*******************************************************************
????created:????2006/07/20
????filename:?????TemplateMethod.h
????author:????????李創
????????????????
http://www.shnenglu.com/converse/
????purpose:????TemplateMethod模式的演示代碼
********************************************************************
*/
//
?抽象基類,定義算法的輪廓
class
?AbstractClass
{
public
:
????AbstractClass()
{}
????
virtual
?
~
AbstractClass()
{}
????
//
?這個函數中定義了算法的輪廓
????
void
?TemplateMethod();
protected
:
????
//
?純虛函數,由派生類實現之
????
virtual
?
void
?PrimitiveOperation1()?
=
?
0
;
????
virtual
?
void
?PrimitiveOperation2()?
=
?
0
;
}
;
//
?繼承自AbstractClass,實現算法
class
?ConcreateClass
????:?
public
?AbstractClass
{
public
:
????ConcreateClass()
{}
????
virtual
?
~
ConcreateClass()
{}
protected
:
????
virtual
?
void
?PrimitiveOperation1();
????
virtual
?
void
?PrimitiveOperation2();
}
;
2)TemplateMethod.cpp
/**/
/*
*******************************************************************
????created:????2006/07/20
????filename:?????TemplateMethod.cpp
????author:????????李創
????????????????
http://www.shnenglu.com/converse/
????purpose:????TemplateMethod模式的演示代碼
********************************************************************
*/
#include?
"
TemplateMethod.h
"
#include?
<
iostream
>
void
?AbstractClass::TemplateMethod()
{
????PrimitiveOperation1();
????PrimitiveOperation2();
}
void
?ConcreateClass::PrimitiveOperation1()
{
????std::cout?
<<
?
"
PrimitiveOperation1?by?ConcreateClass\n
"
;
}
void
?ConcreateClass::PrimitiveOperation2()
{
????std::cout?
<<
?
"
PrimitiveOperation2?by?ConcreateClass\n
"
;
}
3)Main.cpp
/**/
/*
*******************************************************************
????created:????2006/07/20
????filename:?????Main.cpp
????author:????????李創
????????????????
http://www.shnenglu.com/converse/
????purpose:????TemplateMethod模式的測試代碼
********************************************************************
*/
#include?
"
TemplateMethod.h
"
#include?
<
stdlib.h
>
int
?main()
{
????AbstractClass
*
?pConcreateClass?
=
?
new
?ConcreateClass;
????pConcreateClass
->
TemplateMethod();
????delete?pConcreateClass;
????system(
"
pause
"
);
????
return
?
0
;
}
posted on 2006-07-27 23:06
那誰
閱讀(1920)
評論(2)
編輯
收藏
引用
所屬分類:
設計模式
評論
#
re: 常見設計模式的解析和實現(C++)之十一-TemplateMethod模式
回復
更多評論
繼承型的行為模式
和STL算法模板 + functor思想相似
不過一個編譯時靜態、一個運行時動態
2006-07-28 09:46 |
Arcrest
#
re: 常見設計模式的解析和實現(C++)之十一-TemplateMethod模式
回復
更多評論
樓主寫的 這個系列 很好啊 ,我要好好學習了
@Arcrest
2007-03-23 09:24 |
waitng
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
服務器公共庫開發--線程安全的singleton類, 可配置的線程鎖管理類
常見設計模式的解析和實現(C++)文檔及源碼打包下載
常見設計模式的解析和實現(C++)之二十一-完結篇
常見設計模式的解析和實現(C++)之二十-Visitor模式
常見設計模式的解析和實現(C++)之十九-Memento模式
常見設計模式的解析和實現(C++)之十八-Iterator模式
常見設計模式的解析和實現(C++)之十七-State模式
常見設計模式的解析和實現(C++)之十六-Strategy模式
常見設計模式的解析和實現(C++)之十五-Observer模式
常見設計模式的解析和實現(C++)之十四-Command模式
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Powered by:
C++博客
Copyright © 那誰
導航
C++博客
首頁
聯系
聚合
管理
<
2025年6月
>
日
一
二
三
四
五
六
25
26
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
1
2
3
4
5
公告
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
(71)
給我留言
查看公開留言
查看私人留言
隨筆分類
(264)
avidya(1)
(rss)
C\C++(21)
(rss)
ccache(8)
(rss)
CGL(5)
(rss)
eventrpc(1)
(rss)
gdb(2)
(rss)
libevent(2)
(rss)
lighttpd(10)
(rss)
linux kernel(7)
(rss)
Linux/Unix(32)
(rss)
memcached(2)
(rss)
mktags(4)
(rss)
Nginx(5)
(rss)
Perl(3)
(rss)
tokyo cabinet(5)
(rss)
操作系統(1)
(rss)
讀書筆記(3)
(rss)
服務器設計(42)
(rss)
腳本語言(1)
(rss)
經驗教訓(4)
(rss)
其他(10)
(rss)
設計模式(24)
(rss)
算法與數據結構(48)
(rss)
圖形學(1)
(rss)
網絡編程(22)
(rss)
隨筆檔案
(210)
2010年8月 (1)
2010年7月 (3)
2010年6月 (2)
2010年5月 (1)
2010年4月 (2)
2010年3月 (1)
2010年1月 (5)
2009年12月 (7)
2009年11月 (3)
2009年10月 (7)
2009年9月 (2)
2009年8月 (2)
2009年7月 (1)
2009年6月 (3)
2009年5月 (2)
2009年4月 (7)
2009年3月 (2)
2009年2月 (2)
2009年1月 (5)
2008年12月 (1)
2008年11月 (2)
2008年10月 (6)
2008年9月 (12)
2008年8月 (11)
2008年7月 (5)
2008年6月 (2)
2008年4月 (3)
2008年3月 (3)
2008年2月 (1)
2008年1月 (1)
2007年12月 (3)
2007年11月 (3)
2007年8月 (1)
2007年7月 (2)
2007年6月 (2)
2007年5月 (9)
2007年4月 (1)
2007年3月 (8)
2007年2月 (3)
2007年1月 (5)
2006年12月 (4)
2006年11月 (3)
2006年10月 (5)
2006年9月 (4)
2006年8月 (13)
2006年7月 (28)
2006年4月 (1)
2006年3月 (4)
2006年2月 (4)
2006年1月 (1)
2005年12月 (1)
相冊
ccache
lighttpd
tokyo cabinet
文件
關于我
我的google reader share
我的google reader share
開源項目
libevent
lighttpd
memcached
PCRE for Windows (Win32)
sqlite
STLFilt
論壇
ChinaUnix
OldLinux
朋友
cugb_cat
Edengundam
win_hate
ypxing
老羅
搜索
最新評論
1.?re: memcached采用的網絡模型
很好的文章,值得分享。
--紐約網站設計
2.?re: 常見設計模式的解析和實現(C++)文檔及源碼打包下載
評論內容較長,點擊標題查看
--殘陽叢林
3.?re: libevent事件處理框架分析
@hailong
拿走后,堆的恢復是logn
--jiao
4.?re: 從半同步-半異步模式談服務器的設計
學習服務器的一些代碼模式。
--王小亮
5.?re: 讓libevent支持多線程
剛開始以為有個新的方法可以實現多線程。。。。其實就試類似pipe的方式, memcache就是這樣做的,可以參考一下
--fly2010love
閱讀排行榜
1.?同步/異步與阻塞/非阻塞的區別(53106)
2.?libevent事件處理框架分析(45275)
3.?epoll學習筆記(41184)
4.?解讀google C++ code style談對C++的理解(38191)
5.?集成libevent,google protobuf的RPC框架(27508)
6.?常見設計模式的解析和實現(C++)文檔及源碼打包下載(24091)
7.?讓libevent支持多線程(23623)
8.?一個關于臨時對象和虛擬析構函數的問題(22339)
9.?epoll為什么這么快(20317)
10.?二叉樹遍歷算法集合(前中后序遍歷的遞歸和非遞歸算法,層序遍歷算法)(20253)
11.?Callback在C\C++中的實現(20177)
12.?二分查找算法(迭代和遞歸版本)(18345)
13.?談目前項目組的代碼提交制度(18001)
14.?Linux下面的線程鎖,條件變量以及信號量的使用(15688)
15.?C++的流設計很糟糕(14826)
16.?二分查找學習札記(14309)
17.?memcached采用的網絡模型(13975)
18.?紅黑樹的實現源碼(第二次修訂版)(13703)
19.?多進程服務器中,epoll的創建應該在創建子進程之后(12726)
20.?第一個socket程序-C\S模式的文件傳輸程序(12290)
21.?使用tolua++創建基于C\C++語言的lua腳本(12103)
22.?博客遷移(11722)
23.?從半同步-半異步模式談服務器的設計(11619)
24.?Lighty與Nginx的比較分析(11593)
25.?Btree算法實現代碼(11587)
26.?向德國人低頭(11559)
27.?epoll相關資料整理(11293)
28.?把二分查找算法寫正確需要注意的地方(11167)
29.?程序設計經驗總結(10268)
30.?我的項目Makefile文件模板(10191)
31.?帶超時機制的DNS解析API(9586)
32.?方法與工具(9424)
33.?自己設想的一個IM服務器的架構(9209)
評論排行榜
1.?常見設計模式的解析和實現(C++)文檔及源碼打包下載(90)
久久精品国产国产精品四凭
|
久久超乳爆乳中文字幕
|
国产精品99精品久久免费
|
国产成人久久精品一区二区三区
|
久久久久97国产精华液好用吗
|
精品久久久久中文字
|
久久精品国产亚洲AV久
|
69久久精品无码一区二区
|
久久精品女人天堂AV麻
|
欧美大香线蕉线伊人久久
|
国产日韩久久免费影院
|
久久www免费人成看片
|
爱做久久久久久
|
777米奇久久最新地址
|
久久人人添人人爽添人人片牛牛
|
亚洲国产精品久久久久婷婷老年
|
亚洲欧洲精品成人久久奇米网
|
国产91色综合久久免费
|
久久国语露脸国产精品电影
|
久久久中文字幕日本
|
国产精品久久久久久搜索
|
久久久久久久波多野结衣高潮
|
久久综合九色综合久99
|
欧美日韩久久中文字幕
|
亚洲国产成人久久综合一区77
|
久久天堂电影网
|
日本强好片久久久久久AAA
|
性做久久久久久久久久久
|
99久久精品国产一区二区三区
|
999久久久无码国产精品
|
亚洲熟妇无码另类久久久
|
欧美精品九九99久久在观看
|
久久精品国产色蜜蜜麻豆
|
一本一道久久精品综合
|
亚洲国产成人久久精品影视
|
久久精品国产亚洲AV高清热
|
无码AV中文字幕久久专区
|
天天躁日日躁狠狠久久
|
亚洲AV无码1区2区久久
|
亚洲AV成人无码久久精品老人
|
国色天香久久久久久久小说
|