2011年4月13日 星期三

Gof Creational - Flyweight Pattern


  • Intent


想讓某個類別的一個物件實體,可以提供多個 虛擬實體,就可以使用這模式了。

Use sharing to support large numbers of fine-grained objects efficiently.

這個pattern的概念有點像是提供一個pool,放進一些待取用的物件實體,當client有物件需求時,就先到這obj pool裡面先找看看是否有適用的,若沒有的話則產生一個名實相符的物件實體給client,這樣的應用很常發生在cache , service locator中夾雜的使用,我覺得算是概念性的說明了!!


  • Scenario


1. An application uses a considerable number of objects.

2. The storage costs are high because of the quantity of objects.

3. The application does not depend on object identity.

總的來說,就是當我們會需要考慮到同一種類型的物件需要產生多個實體時,但要生成及存放這些物件的成本過高,且client端與這些實體並沒有相依性時,就可以嘗試這麼做了。


  • 常見的API - java.lang.String

  • Class Diagram



  • [caption id="attachment_206" align="alignnone" width="598" caption="FlyweightPattern"]FlyweightPattern[/caption]

沒有留言: