?? flyweightpattern.htm
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>Flyweight 模式</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">Design Pattern: Flyweight 模式</a></h1>
在 <a href="GoF.htm">Gof 的書</a>中指出,Flyweight的目的在于運用共享技術,使得一些細粒度的物件可以共享。<br>
<br>
Flyweight在牛津字典中的解釋是"boxer of the lightest class"。意思是特輕量級拳擊手?其實應該是取"the
lightest
class"這部份的解釋,一個特輕量級類別,這個類別所產生的物件可以共用在每一個場合(context),并依場合資訊表現物件外觀。<br>
<br>
在書中所舉出的例子是文檔編輯器中的字元物件,若每個字元物件會包括字元、大小、字型等等不同的資訊,想想一篇文章中可能出現多少字元,如果我們為每一個
字元都使用一個物件來完整描述有關于它的訊息,那么一篇文字中將會耗用多少的記憶體?!字元本身應可以共享,而大小、字型等等不同的資訊再分別設定。<br>
<br>
考慮數量多且性質相近的物件時,將該物件的資訊分為兩個部份:內部狀態(intrinsic)與外部狀態(extrinsic)。以上例來說,字元屬于內部狀態,而大小、字型等等不同的資訊屬于外部狀態。<br>
<br>
更詳細一些來說明,內部狀態是物件可共享的訊息部份,例如在繪制一個英文字串時,重覆的字元部份為內部狀態,像是 "ABC is
BAC",其中A、B、C的字元資訊部份不必直接儲存于字元物件中,它是屬于可以共享的部份,可以將這些可以重復使用的字元儲存在Flyweight
Pool中。<br>
<br>
外部狀態是物件依賴的一個場景(context),例如繪制字元時的字型資訊、位置資訊等等,繪制一個字元時,先從Flyweight Pool中找出共享的Flyweight,然后從場景中查找對應的繪制資訊(字型、大小、位置等)。<br>
<br>
其實任何學過Java的人就一定使用過Java中運用Flyweight模式的好處,要知道,如果您在程式中使用下面的方式來宣告,則實際上是指向同一個字串物件:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">String str1 = "flyweight";</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">String str2 = "flyweight"; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">System.out.println(str1 == str2);</span><br>
</div>
<br>
程式的執行結果會顯示True,在Java中,會維護一個String Pool,對于一些可以共享的字串物件,會先在String
Pool中查找是否存在相同的String內容(字元相同),如果有就直接傳回,而不是直接創造一個新的String物件,以減少記憶體的耗用。<br>
<br>
再來個一看例子,String的intern()方法,我們來看看它的API說明的節錄:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">Returns a canonical representation for the string object.</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">A pool of strings, initially empty, is maintained privately by the class String.</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">When
the intern method is invoked, if the pool already contains a string
equal to this String object as determined by the equals(Object) method,
then the string from the pool is returned. Otherwise, this String
object is added to the pool and a reference to this String object is
returned.</span><br>
</div>
<br>
這段話其實已說明了Flyweight模式的運作方式,用個實例來說明會更清楚:<br>
<ul>
<li>Main.java</li>
</ul>
<pre>public class Main { <br> public static void main(String[] args) { <br> String str1 = "fly"; <br> String str2 = "weight"; <br> String str3 = "flyweight"; <br> String str4; <br><br> str4 = str1 + str2; <br> System.out.println(str3 == str4); <br><br> str4 = (str1 + str2).intern(); <br> System.out.println(str3 == str4); <br> } <br>}</pre>
<br>
在程式中第一次比較str3與str4物件是否為同一物件時,您知道結果會是false,而intern()方法會先檢查 String
Pool中是否存在字元部份相同的字串物件,如果有的話就傳回,由于程式中之前已經有"flyweight"字串物件,intern()在String
Pool中發現了它,所以直接傳回,這時再進行比較,str3與str4所指向的其實是同一物件,所以結果會是true。<br>
<br>
Flyweight模式在傳回物件時,所使用的是工廠模式,使用者并不會知道物件被創造的細節,下圖是Flyweight模式的結構圖: <br>
<div style="text-align: center;"><img style="width: 450px; height: 265px;" alt="Flyweight" title="Flyweight" src="images/flyweight-1.jpg"><br>
</div>
<br>
之前舉的例子是針對物件的內部狀態所作的說明,那么字型資訊等外部的設定呢?一兩個簡單的外部資訊設定可以直接寫死(hard code)在程式中,例如簡單的使用介面字型設定。<br>
<br>
但如果是文書處理器呢?使用者設定字型、大小等資訊會是動態的呢?Gof書中將字型資訊作為是繪制字元的外部狀態,使用一個Context
物件來維護外部狀態資料庫,每次要繪制字元物件時,這個Context物件會被作為參數傳遞給字元物件,字元物件透過查找Context中的資料來獲得字
型資訊,從而進行正確的場景繪制。<br>
<br>
外部狀態維護與內部狀態之間的對應關系,在查找時,Gof書中所使用的是BTree?結構,由于查找必須花費時間,所以這也指出了使用Flyweight
模式所必須付出的代價:以時間換取空間。如何設計外部狀態的資料結構,以使得查找時間縮短,這是另一個重要的課題(不過就不是這篇文章要討論的課題了)。<br>
<br>
補充:關于字元(內部狀態)及字型、大小(外部狀態)之間的對應問題通常不太需要程式設計人員的關心,因為通常可以找的到一些現成的圖型介面API,它們都設計好一些相關元件,直接使用就可以了。<br>
<br>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -