複製鏈接
請複製以下鏈接發送給好友

Flyweight

鎖定
Flyweight,中文翻譯為輕量級的意思,適用於windows系統。
中文名
Flyweight
來    源
面嚮對象語言
使用環境
windows

FlyweightFlyweight模式定義

設計模式中的享元模式,避免大量擁有相同內容的小類的開銷(如耗費內存),使大家共享一個類(元類).

Flyweight為什麼使用

面嚮對象語言的原則就是一切都是對象,但是如果真正使用起來,有時對象數可能顯得很龐大,比如,字處理軟件,如果以每個文字都作為一個對象,幾千個字,對象數就是幾千,無疑耗費內存,那麼我們還是要"求同存異",找出這些對象羣的共同點,設計一個元類,封裝可以被共享的類,另外,還有一些特性是取決於應用(context),是不可共享的,這也是Flyweight中兩個重要概念--內部狀態intrinsic和外部狀態extrinsic之分。
説白點,就是先捏一個的原始模型,然後隨着不同場合和環境,再產生各具特徵的具體模型,很顯然,在這裏需要產生不同的新對象,所以Flyweight模式中常出現Factory模式。Flyweight的內部狀態是用來共享的,Flyweight factory負責維護一個Flyweight pool(模式池)來存放內部狀態的對象。
Flyweight模式是一個提高程序效率和性能的模式,會大大加快程序的運行速度。應用場合很多:比如你要從一個數據庫中讀取一系列字符串,這些字符串中有許多是重複的,那麼我們可以將這些字符串儲存在Flyweight池(pool)中。

Flyweight使用方法

我們先從Flyweight抽象接口開始:
public interface Flyweight
{
public void operation( ExtrinsicState state );
}
//用於本模式的抽象數據類型(自行設計)
public interface ExtrinsicState { }
下面是接口的具體實現(ConcreteFlyweight) ,併為內部狀態增加內存空間, ConcreteFlyweight必須是可共享的,它保存的任何狀態都必須是內部(intrinsic),也就是説,ConcreteFlyweight必須和它的應用環境場合無關.
public class ConcreteFlyweight implements Flyweight {
private IntrinsicState state;
public void operation( ExtrinsicState state )
{

Flyweight具體操作

}
}
當然,並不是所有的Flyweight具體實現子類都需要被共享的,所以還有另外一種不共享的ConcreteFlyweight:
public class UnsharedConcreteFlyweight implements Flyweight {
public void operation( ExtrinsicState state ) { }
}
Flyweight factory負責維護一個Flyweight池(存放內部狀態),當客户端請求一個共享Flyweight時,這個factory首先搜索池中是否已經有可適用的,如果有,factory只是簡單返回送出這個對象,否則,創建一個新的對象,加入到池中,再返回送出這個對象.池
public class FlyweightFactory {
//Flyweight pool
private Hashtable flyweights = new Hashtable();
public Flyweight getFlyweight( Object key ) {
Flyweight flyweight = (Flyweight) flyweights.get(key);
if( flyweight == null ) {
//產生新的ConcreteFlyweight
flyweight = new ConcreteFlyweight();
flyweights.put( key, flyweight );
}
return flyweight;
}
}
至此,Flyweight模式的基本框架已經就緒,我們看看如何調用:
FlyweightFactory factory = new FlyweightFactory();
Flyweight fly1 = factory.getFlyweight( "Fred" );
Flyweight fly2 = factory.getFlyweight( "Wilma" );
......
從調用上看,好象是個純粹的Factory使用,但奧妙就在於Factory的內部設計上.
Flyweight模式在XML等數據源中應用
我們上面已經提到,當大量從數據源中讀取字符串,其中肯定有重複的,那麼我們使用Flyweight模式可以提高效率,以唱片CD為例,在一個XML文件中,存放了多個CD的資料.
每個CD有三個字段:
1.出片日期(year)
2.歌唱者姓名等信息(artist)
3.唱片曲目 (title)
其中,歌唱者姓名有可能重複,也就是説,可能有同一個演唱者的多個不同時期 不同曲目的CD.我們將"歌唱者姓名"作為可共享的ConcreteFlyweight.其他兩個字段作為UnsharedConcreteFlyweight.
首先看看數據源XML文件的內容:
<?xml version="1.0"?>
<collection>
<cd>
<title>Another Green World</title>
<year>1978</year>
<artist>Eno, Brian</artist>
</cd>
<cd>
<title>Greatest Hits</title>
<year>1950</year>
<artist>Holiday, Billie</artist>
</cd>
<cd>
<title>Taking Tiger Mountain (by strategy)</title>
<year>1977</year>
<artist>Eno, Brian</artist>
</cd>
.......
</collection>
雖然上面舉例CD只有3張,CD可看成是大量重複的小類,因為其中成分只有三個字段,而且有重複的(歌唱者姓名).
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 {
//內部狀態
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);
////產生新的Artist
if(result == null) {
result = new Artist(key);
pool.put(key,result);
}
return result;
}
}
當你有幾千張甚至更多CD時,Flyweight模式將節省更多空間,共享的flyweight越多,空間節省也就越大.