蝸牛的家
男兒當(dāng)自強(qiáng)
C++博客
首頁
新文章
新隨筆
聚合
管理
posts - 48, comments - 21, trackbacks - 0
C++模式-FlyWeight
意圖:
運(yùn)用共享技術(shù)有效地支持大量細(xì)粒度的對(duì)象
適用:
一個(gè)應(yīng)用程序使用了大量的對(duì)象
完全由于使用大量的對(duì)象,造成很大的存儲(chǔ)開銷
對(duì)象的大多數(shù)狀態(tài)都可變?yōu)橥獠繝顟B(tài)
如果刪除對(duì)象的外部狀態(tài),那么可以用相對(duì)較少的共享對(duì)象取代很多組對(duì)象
UML
解析:
Flywweight模式大量使用在當(dāng)一些可以被共享的對(duì)象經(jīng)常使用的情況下
//
test.h
#include
<
string
>
#include
<
list
>
/**/
////////////////////////////////////////////////////////////////////////
//
using
namespace
std;
class
Flyweight
{
public
:
virtual
~
Flyweight()
{}
string
GetIntrinsicState();
virtual
void
Operation(
string
&
ExtrinsicState)
=
0
;
protected
:
Flyweight(
const
string
&
state) : m_state(state)
{}
private
:
string
m_state;
}
;
class
FlyweightFactory
{
public
:
FlyweightFactory()
{}
~
FlyweightFactory();
Flyweight
*
GetFlyweight(
const
string
&
key);
private
:
list
<
Flyweight
*>
m_listFlyweight;
}
;
class
ConCreateFlyweight :
public
Flyweight
{
public
:
ConCreateFlyweight(
const
string
&
state) : Flyweight(state)
{}
virtual
~
ConCreateFlyweight()
{}
virtual
void
Operation(
string
&
ExtrinsicState);
}
;
//
test.cpp : Defines the entry point for the console application.
//
#include
"
stdafx.h
"
#include
<
iostream
>
#include
"
test.h
"
using
namespace
std;
/**/
////////////////////////////////////////////////////////////////////////
//
inline
string
Flyweight::GetIntrinsicState()
{
return
m_state;
}
FlyweightFactory::
~
FlyweightFactory()
{
list
<
Flyweight
*>
::iterator iter1,iter2,temp;
for
(iter1
=
m_listFlyweight.begin(),iter2
=
m_listFlyweight.end(); iter1
!=
iter2;)
{
temp
=
iter1;
++
iter1;
delete(
*
temp);
}
m_listFlyweight.clear();
}
Flyweight
*
FlyweightFactory::GetFlyweight(
const
string
&
key)
{
list
<
Flyweight
*>
::iterator iter1,iter2;
//
查看列表中是否有存在的對(duì)象
for
(iter1
=
m_listFlyweight.begin(),iter2
=
m_listFlyweight.end(); iter1
!=
iter2;
++
iter1)
{
if
((
*
iter1)
->
GetIntrinsicState()
==
key)
{
cout
<<
"
The Flyweight:
"
<<
key
<<
"
already exits
"
<<
endl;
return
(
*
iter1);
}
}
cout
<<
"
Creating a new Flyweight:
"
<<
key
<<
endl;
Flyweight
*
pFlyweight
=
new
ConCreateFlyweight(key);
m_listFlyweight.push_back(pFlyweight);
}
void
ConCreateFlyweight::Operation(
string
&
ExtrinsicState)
{
}
/**/
////////////////////////////////////////////////////////////////////////
//
int
main(
int
argc,
char
*
argv[])
{
FlyweightFactory flyweightFactory;
flyweightFactory.GetFlyweight(
"
hello
"
);
flyweightFactory.GetFlyweight(
"
world
"
);
flyweightFactory.GetFlyweight(
"
hello
"
);
system(
"
pause
"
);
return
0
;
}
posted on 2008-08-20 22:54
黑色天使
閱讀(483)
評(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)容較長,點(diǎn)擊標(biāo)題查看
--no7dw
2.?re: YUV格式詳細(xì)解釋與FFMPEG的關(guān)系
評(píng)論內(nèi)容較長,點(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)容較長,點(diǎn)擊標(biāo)題查看
--tangxinfa
閱讀排行榜
1.?RGB、YUY2、YUYV、YVYU、UYVY與AYUV(轉(zhuǎn))(6689)
2.?YUV格式詳細(xì)解釋與FFMPEG的關(guān)系(4311)
3.?如何檢測(cè)內(nèi)存泄漏(轉(zhuǎn))(3904)
4.?memcpy的BUG(2717)
5.?內(nèi)存池技術(shù)學(xué)習(xí)筆記(2353)
評(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浪潮
|
无码任你躁久久久久久久
|
2021国内久久精品
|
99久久精品国产麻豆
|
久久久久香蕉视频
|
久久夜色精品国产噜噜麻豆
|
久久久久久免费一区二区三区
|
久久亚洲国产成人影院网站
|
久久精品国产99国产电影网
|
人妻少妇精品久久
|
久久久久AV综合网成人
|
欧美午夜A∨大片久久
|
久久成人精品视频
|
亚洲精品乱码久久久久久久久久久久
|
亚洲国产精品无码久久久蜜芽
|
无码八A片人妻少妇久久
|
99久久国产综合精品五月天喷水
|
伊人伊成久久人综合网777
|
热久久这里只有精品
|
久久狠狠爱亚洲综合影院
|
久久精品国产精品亚洲人人
|
久久久亚洲欧洲日产国码aⅴ
|
欧美日韩精品久久久久
|
久久香蕉综合色一综合色88
|
亚洲精品国产美女久久久
|
四虎影视久久久免费观看
|
国产巨作麻豆欧美亚洲综合久久
|
性欧美大战久久久久久久久
|
久久这里的只有是精品23
|
久久久噜噜噜久久
|
segui久久国产精品
|
精品久久久久久成人AV
|
久久99国内精品自在现线
|
亚洲香蕉网久久综合影视
|
久久亚洲2019中文字幕
|
久久夜色精品国产噜噜亚洲a
|
亚洲午夜精品久久久久久浪潮
|
国产精品久久久久久久午夜片
|