蝸牛的家
男兒當自強
C++博客
首頁
新文章
新隨筆
聚合
管理
posts - 48, comments - 21, trackbacks - 0
C++設計模式-Prototype
意圖:
用原型指定創建對象的種類,并且通過拷貝函數用這些原型創建對象
適用:
當要實例化的類是在運行時刻指定時,例如:通過動態裝載
為了避免創建一個與產品類層次平行的工廠類層次時
當一個類的實例只能有幾個不同狀態組合中的一種時,建立相應數目的原型并克隆可能要更方便些
UML圖:
解析:Prototype類似車輛的展示,當你喜歡某款車時,他們會給你一款相同款式的車,像軟件中的復制,而不是車展的那輛
//
test.h
/**/
////////////////////////////////////////////////////////////////////////
//
//
虛基類,提供基本函數
class
Prototype
{
public
:
Prototype()
{}
virtual
~
Prototype()
{}
virtual
Prototype
*
Clone()
=
0
;
}
;
//
派生出來的類,實現具體的Clone函數
class
ConCreatePrototype1 :
public
Prototype
{
public
:
ConCreatePrototype1();
ConCreatePrototype1(
const
ConCreatePrototype1
&
);
//
拷貝構造函數,通過這個函數實現類的復制
~
ConCreatePrototype1();
virtual
Prototype
*
Clone();
//
復制出相同對象的接口
}
;
//
test.cpp : Defines the entry point for the console application.
//
#include
"
stdafx.h
"
#include
<
iostream
>
#include
"
stdlib.h
"
#include
"
test.h
"
using
namespace
std;
/**/
////////////////////////////////////////////////////////////////////////
//
ConCreatePrototype1::ConCreatePrototype1()
{
cout
<<
"
construction of ConCreatePrototype1\n
"
;
}
ConCreatePrototype1::
~
ConCreatePrototype1()
{
cout
<<
"
destruction of ConCreatePrototype1\n
"
;
}
ConCreatePrototype1::ConCreatePrototype1(
const
ConCreatePrototype1
&
)
{
cout
<<
"
copy a same object as ConCreatePrototype1\n
"
;
}
Prototype
*
ConCreatePrototype1::Clone()
{
return
new
ConCreatePrototype1(
*
this
);
}
/**/
////////////////////////////////////////////////////////////////////////
//
int
main(
int
argc,
char
*
argv[])
{
Prototype
*
pPrototype
=
new
ConCreatePrototype1;
Prototype
*
pPrototype2
=
pPrototype
->
Clone();
delete pPrototype;
delete pPrototype2;
system(
"
pause
"
);
return
0
;
}
posted on 2008-08-17 01:16
黑色天使
閱讀(539)
評論(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(轉)(6703)
2.?YUV格式詳細解釋與FFMPEG的關系(4320)
3.?如何檢測內存泄漏(轉)(3908)
4.?memcpy的BUG(2730)
5.?內存池技術學習筆記(2361)
評論排行榜
1.?CHttpDownLoad Beta 1.0(10)
2.?memcpy的BUG(5)
3.?事件模型SOCKET封裝(2)
4.?鍵盤過濾驅動源代碼(2)
5.?C++設計模式-Observer(1)
Copyright ©2025 黑色天使 Powered By
博客園
模板提供:
滬江博客
伊人色综合久久
|
色综合久久无码五十路人妻
|
A级毛片无码久久精品免费
|
99久久99久久精品国产
|
亚洲国产天堂久久综合
|
久久久久久国产精品无码超碰
|
久久精品一本到99热免费
|
久久精品国产黑森林
|
亚洲色欲久久久综合网
|
久久国产精品二国产精品
|
久久精品欧美日韩精品
|
中文字幕无码久久精品青草
|
久久永久免费人妻精品下载
|
欧美成a人片免费看久久
|
国产亚洲欧美精品久久久
|
老司机午夜网站国内精品久久久久久久久
|
久久精品中文无码资源站
|
99久久国产热无码精品免费久久久久
|
人妻丰满?V无码久久不卡
|
91久久香蕉国产熟女线看
|
亚洲AV成人无码久久精品老人
|
精品久久人人妻人人做精品
|
国产综合久久久久
|
久久亚洲日韩精品一区二区三区
|
久久久久久精品无码人妻
|
日本三级久久网
|
久久国产成人精品麻豆
|
99精品久久精品一区二区
|
久久久一本精品99久久精品88
|
国产AⅤ精品一区二区三区久久
|
国产精品99久久不卡
|
久久精品国产亚洲网站
|
久久精品国产亚洲AV电影
|
久久综合狠狠综合久久
|
久久婷婷五月综合色奶水99啪
|
伊人色综合九久久天天蜜桃
|
青青草原综合久久大伊人导航
|
精品人妻伦一二三区久久
|
久久久99精品一区二区
|
久久精品成人欧美大片
|
99久久综合国产精品免费
|