蝸牛的家
男兒當自強
C++博客
首頁
新文章
新隨筆
聚合
管理
posts - 48, comments - 21, trackbacks - 0
C++模式-FlyWeight
意圖:
運用共享技術有效地支持大量細粒度的對象
適用:
一個應用程序使用了大量的對象
完全由于使用大量的對象,造成很大的存儲開銷
對象的大多數(shù)狀態(tài)都可變?yōu)橥獠繝顟B(tài)
如果刪除對象的外部狀態(tài),那么可以用相對較少的共享對象取代很多組對象
UML
解析:
Flywweight模式大量使用在當一些可以被共享的對象經(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;
//
查看列表中是否有存在的對象
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
黑色天使
閱讀(486)
評論(0)
編輯
收藏
引用
所屬分類:
設計模式
只有注冊用戶
登錄
后才能發(fā)表評論。
【推薦】100%開源!大型工業(yè)跨平臺軟件C++源碼提供,建模,組態(tài)!
相關文章:
decorator模式
MVC模式理解——當年給我一個browser多好(轉)
C++設計模式-趣解
C++設計模式-visitor
C++設計模式-Memento
C++模式-Iterator
C++設計模式-Observer
C++設計模式-Command
C++模式-FlyWeight
C++設計模式-ChainOfResponsibility
網(wǎng)站導航:
博客園
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)
操作系統(tǒng)(1)
多進程&多線程
流媒體開發(fā)
內存管理技術(2)
軟件工程(1)
設計模式(20)
數(shù)據(jù)結構&算法(2)
網(wǎng)絡開發(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)
搜索
最新評論
1.?re: C++設計模式-Observer
評論內容較長,點擊標題查看
--no7dw
2.?re: YUV格式詳細解釋與FFMPEG的關系
評論內容較長,點擊標題查看
--windsome
3.?re: 鍵盤過濾驅動源代碼
@soul
再怎么懶也應該自己實現(xiàn)一部分吧
--黑色天使
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
博客園
模板提供:
滬江博客
久久精品国产精品亚洲下载
|
九九精品99久久久香蕉
|
久久WWW免费人成一看片
|
99久久超碰中文字幕伊人
|
人人狠狠综合88综合久久
|
九九久久精品国产
|
伊人久久大香线蕉综合Av
|
久久婷婷五月综合色奶水99啪
|
久久精品蜜芽亚洲国产AV
|
岛国搬运www久久
|
青青草原综合久久大伊人
|
狠狠色丁香婷综合久久
|
一本色道久久综合
|
久久婷婷五月综合国产尤物app
|
国产成人精品久久一区二区三区av
|
国产精品久久久久乳精品爆
|
久久人人爽人人爽人人片av麻烦
|
996久久国产精品线观看
|
久久99热这里只频精品6
|
国产精品久久久久一区二区三区
|
久久久无码精品亚洲日韩按摩
|
久久人人爽人人爽人人片AV东京热
|
国产一久久香蕉国产线看观看
|
久久亚洲中文字幕精品一区
|
好属妞这里只有精品久久
|
国产毛片欧美毛片久久久
|
久久人妻少妇嫩草AV蜜桃
|
色综合久久88色综合天天
|
久久亚洲精品成人av无码网站
|
人妻丰满?V无码久久不卡
|
国产一区二区三精品久久久无广告
|
狠狠色婷婷久久一区二区
|
香蕉aa三级久久毛片
|
久久精品国产精品亚洲
|
久久99精品久久久久久不卡
|
88久久精品无码一区二区毛片
|
国内精品久久人妻互换
|
国产三级久久久精品麻豆三级
|
久久亚洲综合色一区二区三区
|
99久久无码一区人妻a黑
|
无码人妻少妇久久中文字幕蜜桃
|