- 論壇徽章:
- 0
|
Flyweight 定義:
避免大量擁有相同內(nèi)容的小類的開銷(如耗費(fèi)內(nèi)存),使大家共享一個(gè)類(元類).
為什么使用?
面向?qū)ο笳Z言的原則就是一切都是對象,但是如果真正使用起來,有時(shí)對象數(shù)可能顯得很龐
大,比如,字處理軟件,如果以每個(gè)文字都作為一個(gè)對象,幾千個(gè)字,對象數(shù)就是幾千,無疑耗
費(fèi)內(nèi)存,那么我們還是要"求同存異",找出這些對象群的共同點(diǎn),設(shè)計(jì)一個(gè)元類,封裝可以被
共享的類,另外,還有一些特性是取決于應(yīng)用(context),是不可共享的,這也Flyweight 中兩
個(gè)重要概念內(nèi)部狀態(tài)intrinsic 和外部狀態(tài)extrinsic 之分.
說白點(diǎn),就是先捏一個(gè)的原始模型,然后隨著不同場合和環(huán)境,再產(chǎn)生各具特征的具體模型,
很顯然,在這里需要產(chǎn)生不同的新對象,所以Flyweight 模式中常出現(xiàn)Factory 模
式.Flyweight 的內(nèi)部狀態(tài)是用來共享的,Flyweight factory 負(fù)責(zé)維護(hù)一個(gè)Flyweight
pool(模式池)來存放內(nèi)部狀態(tài)的對象.
Flyweight 模式是一個(gè)提高程序效率和性能的模式,會大大加快程序的運(yùn)行速度.應(yīng)用場合
很多:比如你要從一個(gè)數(shù)據(jù)庫中讀取一系列字符串,這些字符串中有許多是重復(fù)的,那么我們
可以將這些字符串儲存在Flyweight 池(pool)中.
如何使用?
我們先從Flyweight 抽象接口開始:
public interface Flyweight
{
public void operation( ExtrinsicState state );
}
//用于本模式的抽象數(shù)據(jù)類型(自行設(shè)計(jì))
public interface ExtrinsicState { }
下面是接口的具體實(shí)現(xiàn)(ConcreteFlyweight) ,并為內(nèi)部狀態(tài)增加內(nèi)存空間,
ConcreteFlyweight 必須是可共享的,它保存的任何狀態(tài)都必須是內(nèi)部(intrinsic),也就是
說,ConcreteFlyweight 必須和它的應(yīng)用環(huán)境場合無關(guān).
public class ConcreteFlyweight implements Flyweight {
private IntrinsicState state;
public void operation( ExtrinsicState state )
{
//具體操作
}
}
當(dāng)然,并不是所有的Flyweight 具體實(shí)現(xiàn)子類都需要被共享的,所以還有另外一種不共享的
ConcreteFlyweight:
public class UnsharedConcreteFlyweight implements Flyweight {
public void operation( ExtrinsicState state ) { }
}
Flyweight factory 負(fù)責(zé)維護(hù)一個(gè)Flyweight 池(存放內(nèi)部狀態(tài)),當(dāng)客戶端請求一個(gè)共享
Flyweight 時(shí),這個(gè)factory 首先搜索池中是否已經(jīng)有可適用的,如果有,factory 只是簡單返
回送出這個(gè)對象,否則,創(chuàng)建一個(gè)新的對象,加入到池中,再返回送出這個(gè)對象.池
public class FlyweightFactory {
//Flyweight pool
private Hashtable flyweights = new Hashtable();
public Flyweight getFlyweight( Object key ) {
Flyweight flyweight = (Flyweight) flyweights.get(key);
if( flyweight == null ) {
//產(chǎn)生新的ConcreteFlyweight
flyweight = new ConcreteFlyweight();
flyweights.put( key, flyweight );
}
return flyweight;
}
}
至此,Flyweight 模式的基本框架已經(jīng)就緒,我們看看如何調(diào)用:
FlyweightFactory factory = new FlyweightFactory();
Flyweight fly1 = factory.getFlyweight( "Fred" );
Flyweight fly2 = factory.getFlyweight( "Wilma" );
......
從調(diào)用上看,好象是個(gè)純粹的Factory 使用,但奧妙就在于Factory 的內(nèi)部設(shè)計(jì)上.
Flyweight 模式在XML 等數(shù)據(jù)源中應(yīng)用
我們上面已經(jīng)提到,當(dāng)大量從數(shù)據(jù)源中讀取字符串,其中肯定有重復(fù)的,那么我們使用
Flyweight 模式可以提高效率,以唱片CD 為例,在一個(gè)XML 文件中,存放了多個(gè)CD 的資料.
每個(gè)CD 有三個(gè)字段:
1.出片日期(year)
2.歌唱者姓名等信息(artist)
3.唱片曲目 (title)
其中,歌唱者姓名有可能重復(fù),也就是說,可能有同一個(gè)演唱者的多個(gè)不同時(shí)期 不同曲目的
CD.我們將"歌唱者姓名"作為可共享的ConcreteFlyweight.其他兩個(gè)字段作為
UnsharedConcreteFlyweight.
首先看看數(shù)據(jù)源XML 文件的內(nèi)容:
Another Green World
1978
Eno, Brian
Greatest Hits
1950
Holiday, Billie
Taking Tiger Mountain (by strategy)
1977
Eno, Brian
.......
雖然上面舉例CD 只有3 張,CD 可看成是大量重復(fù)的小類,因?yàn)槠渲谐煞种挥腥齻(gè)字段,而且
有重復(fù)的(歌唱者姓名).
CD 就是類似上面接口 Flyweight:
public class CD {
private String title;
private int year;
private Artist artist;
public String getTitle() { return title; }
public int getYear() { return year; }
public Artist getArtist() { return artist; }
public void setTitle(String t){ title = t;}
public void setYear(int y){year = y;}
public void setArtist(Artist a){artist = a;}
}
將"歌唱者姓名"作為可共享的ConcreteFlyweight:
public class Artist {
//內(nèi)部狀態(tài)
private String name;
// note that Artist is immutable.
String getName(){
return name;
}
Artist(String n){
name = n;
}
}
再看看Flyweight factory,專門用來制造上面的可共享的ConcreteFlyweight:Artist
public class ArtistFactory {
Hashtable pool = new Hashtable();
Artist getArtist(String key){
Artist result;
result = (Artist)pool.get(key);
////產(chǎn)生新的Artist
if(result == null) {
result = new Artist(key);
pool.put(key,result);
}
return result;
}
}
當(dāng)你有幾千張甚至更多CD 時(shí),Flyweight 模式將節(jié)省更多空間,共享的flyweight 越多,空間
節(jié)省也就越大.
本文來自ChinaUnix博客,如果查看原文請點(diǎn):http://blog.chinaunix.net/u2/67175/showart_2144107.html |
|