一、享元模式簡介(Brief Introduction)
享元模式(Flyweight Pattern),運用共享技術有效支持大量細粒度的對象。
Use sharing to support large numbers of fine-grained objects efficiently.
享元模式可以避免大量非常相似類的開銷。在程序設計中有時需要生成大量細粒度的類 實例來表示數據。如果發現這些實例除了幾個參數外基本傷都是相同的,有時就能夠受大幅 度第減少需要實例化的類的數量。如果能把這些參數移到類實例外面,在方法調用時將他們 傳遞進來,就可以通過共享大幅度地減少單個實例的數目。
享元對象的內部狀態與外部狀態:
內部狀態,在享元對象的內部並且不會隨環境改變而改變的共享部分。
外部狀態,隨環境改變而改變的,不可以共享的狀態。
二、解決的問題(What To Solve)
如果一個應用程序使用了大量的對象,而大量的這些對象造成了很大的存儲開銷,這時 可以考慮使用享元模式。
當對象的大多數狀態是外部狀態,如果刪除對象的外部狀態,那麼可以用相對較少的共 享對象取代很多組對象,這時也可以考慮使用享元模式。
三、享元模式分析(Analysis)
1、享元模式結構
FlyweightFactory類:享元工廠,用來創建和管理Flyweight對象。如果請求的Flyweight 對象存在,怎返回已經存在的對象。否則新創建一個新的對象返回。
Flyweight類:享元抽象類,通過這個接口,Flyweight可以接受並作用與外部狀態。
UnsharedConcreteFlyweight類:不需要共享的Flyweight子類。Flyweight接口並不強制 共享。
ConcreteFlyweight類:實現享元抽象類,為內部狀態添加存儲空間。
2、代碼
1、享元工廠類FlyweightFactory
public class FlyweightFactory
{
public Hashtable flyweights = new Hashtable();
public FlyweightFactory()
{
flyweights.Add("A", new ConcreteFlyweight());
flyweights.Add("B", new ConcreteFlyweight());
flyweights.Add("C", new ConcreteFlyweight());
}
public Flyweight GetFlyweight(string key)
{
return flyweights[key] as Flyweight;
}
}
2、享元抽象類Flyweight 及其具體實現類UnsharedConcreteFlyweight和 ConcreteFlyweight
public abstract class Flyweight
{
public abstract void Operation(int extrinsicstate);
}
public class UnsharedConcreteFlyweight:Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("{0}:{1}",this.GetType ().Name,extrinsicstate);
}
}
public class ConcreteFlyweight:Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("{0}:{1}", this.GetType().Name, extrinsicstate);
}
}
3、客戶端代碼
static void Main(string[] args)
{
// Arbitrary extrinsic state
int extrinsicstate = 20;
FlyweightFactory factory = new FlyweightFactory();
// Work with different flyweight instances
Flyweight fx = factory.GetFlyweight("A");
fx.Operation(--extrinsicstate);
Flyweight fy = factory.GetFlyweight("B");
fy.Operation(--extrinsicstate);
Flyweight fz = factory.GetFlyweight("C");
fz.Operation(--extrinsicstate);
UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight ();
fu.Operation(--extrinsicstate);
Console.ReadKey();
}
3、實例運行結果
四.享元模式實例分析(Example)
1、場景
一個文檔Document中只有少數字符需要共享。結構如下圖所示
CharacterFactory類,享元工廠,用來創建和管理Charactor對象。如果請求的 Charactor對象存在,怎返回已經存在的對象。否則新創建一個新的對象返回。
Character類:享元抽象類,通過這個接口,Character可以接受並作用與外部狀態。
CharacterA /CharacterB/CharacterC 類:實現享元抽象類,為內部狀態添加存儲空間 。
2、代碼
1、字符工廠類CharacterFactory
class CharacterFactory
{
private Dictionary<char, Character> _characters = new Dictionary<char, Character>();
public Character GetCharacter(char key)
{
// Uses "lazy initialization"
Character character = null;
if (_characters.ContainsKey(key))
{
character = _characters[key];
}
else
{
switch (key)
{
case 'A': character = new CharacterA(); break;
case 'B': character = new CharacterB(); break;
//...
case 'Z': character = new CharacterZ(); break;
}
_characters.Add(key, character);
}
return character;
}
}
2、抽象數據對象類DataObject及其具體實現類CustomersData
/// <summary>
/// The 'Flyweight' abstract class
/// </summary>
abstract class Character
{
protected char symbol;
protected int width;
protected int height;
protected int ascent;
protected int descent;
protected int pointSize;
public abstract void Display(int pointSize);
}
/// <summary>
/// A 'ConcreteFlyweight' class
/// </summary>
class CharacterA : Character
{
public CharacterA()
{
this.symbol = 'A';
this.height = 100;
this.width = 120;
this.ascent = 70;
this.descent = 0;
}
public override void Display(int pointSize)
{
this.pointSize = pointSize;
Console.WriteLine(this.symbol + " (pointsize " + this.pointSize + ")");
}
}
/// <summary>
/// A 'ConcreteFlyweight' class
/// </summary>
class CharacterB : Character
{
public CharacterB()
{
this.symbol = 'B';
this.height = 100;
this.width = 140;
this.ascent = 72;
this.descent = 0;
}
public override void Display(int pointSize)
{
this.pointSize = pointSize;
Console.WriteLine(this.symbol + " (pointsize " + this.pointSize + ")");
}
}
// ... C, D, E, etc.
/// <summary>
/// A 'ConcreteFlyweight' class
/// </summary>
class CharacterZ : Character
{
// Constructor
public CharacterZ()
{
this.symbol = 'Z';
this.height = 100;
this.width = 100;
this.ascent = 68;
this.descent = 0;
}
public override void Display(int pointSize)
{
this.pointSize = pointSize;
Console.WriteLine(this.symbol +" (pointsize " + this.pointSize + ")");
}
}
3、客戶端代碼
static void Main(string[] args)
{
// Build a document with text
string document = "AAZZBBZB";
char[] chars = document.ToCharArray();
CharacterFactory factory = new CharacterFactory();
// extrinsic state
int pointSize = 10;
// For each character use a flyweight object
foreach (char c in chars)
{
pointSize++;
Character character = factory.GetCharacter(c);
character.Display(pointSize);
}
Console.ReadKey();
}
3、實例運行結果
五、總結(Summary)
本文對享元模式(Flyweight Pattern)的概念、設計結構圖、代碼、使用場景、進行了 描述。以一個享元模式實例進行了說明。如果一個應用程序使用了大量的對象,而大量的這 些對象造成了很大的存儲開銷,這時可以考慮使用享元模式。
出處:http://www.cnblogs.com/ywqu