C#设计模式系列:享元模式(Flyweight)
當頻繁地從數據源讀取數據時,讀出的內容存在重復,那么需要使用享元模式(Flyweight)來提高內存效率,Flyweight模式將節省更多空間,共享的Flyweight越多,空間節省越大。
1、享元模式簡介
1.1>、定義
享元模式(Flyweight)的存在是為了避免大量擁有相同內容的小類的開銷(如內存開銷),使大家共享一個類。
1.2>、使用頻率
? 低
2、享元模式結構
2.1>、結構圖
?
2.2>、參與者
享元模式參與者:
? Flyweight:聲明一個接口,通過這個接口flyweight可以直接接收并作用于外部狀態。
? ConcreteFlyweight:實現Flyweight接口,并為內部狀態增加存儲空間。ConcreteFlyweight對象必須是可以共享的,它所存儲的狀態必須是內部的,即它必須獨立于ConcreteFlyweight對象的場景。
? UnsharedConcreteFlyweight:并非所有的Flyweight子類都需要被共享。Flyweight接口使共享成為可能,但它不強制共享。在Flyweight對象結構的某些層次,UnsharedConcreteFlyweight對象通常將ConcreteFlyweight對象作為子節點。
? FlyweightFactory
° 創建和管理flyweight對象。
° 確保flyweight對象被合理共享。當Client請求一個flyweight對象時,FlyweightFactory需要可以進行分配,若flyweight對象不存在時,則先創建一個。
? Client:維持一個對Flyweight的引用
在享元模式中,Client調用Flyweight下的ConcreteFlyweight,如果ConcreteFlyweight存在則調用成功;否則就調用FlyweightFactory生產所需要的繼承Flyweight接口的ConcreteFlyweight,以供調用。
3、享元模式結構實現
Flyweight.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace DesignPatterns.FlyweightPattern.Structural {public abstract class Flyweight{public abstract void Operation(int extrinsicstate);} }ConcreteFlyweight.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace DesignPatterns.FlyweightPattern.Structural {public class ConcreteFlyweight : Flyweight{public override void Operation(int extrinsicstate){Console.WriteLine("ConcreteFlyweight: " + extrinsicstate);}} }UnsharedConcreteFlyweight.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace DesignPatterns.FlyweightPattern.Structural {public class UnsharedConcreteFlyweight : Flyweight{public override void Operation(int extrinsicstate){Console.WriteLine("UnsharedConcreteFlyweight: " + extrinsicstate);}} }FlyweightFactory.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;using System.Collections;namespace DesignPatterns.FlyweightPattern.Structural {public class FlyweightFactory{private Hashtable flyweights = new Hashtable();public FlyweightFactory(){flyweights.Add("X", new ConcreteFlyweight());flyweights.Add("Y", new ConcreteFlyweight());flyweights.Add("Z", new ConcreteFlyweight());}public Flyweight GetFlyweight(string key){return ((Flyweight)flyweights[key]);}} }Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;using DesignPatterns.FlyweightPattern.Structural;namespace DesignPatterns.FlyweightPattern {class Program{static void Main(string[] args){// Arbitrary extrinsic stateint extrinsicstate = 22;FlyweightFactory factory = new FlyweightFactory();// Work with different flyweight instancesFlyweight fx = factory.GetFlyweight("X");fx.Operation(--extrinsicstate);Flyweight fy = factory.GetFlyweight("Y");fy.Operation(--extrinsicstate);Flyweight fz = factory.GetFlyweight("Z");fz.Operation(--extrinsicstate);UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight();fu.Operation(--extrinsicstate);}} }運行輸出:
ConcreteFlyweight: 21 ConcreteFlyweight: 20 ConcreteFlyweight: 19 UnsharedConcreteFlyweight: 18 請按任意鍵繼續. . .4、享元模式應用分析
享元模式適用情形:
? 一個應用程序使用了大量的對象
? 完全由于使用大量的對象,造成很大的存儲開銷
? 對象的大多數狀態都可變為外部狀態
? 如果刪除對象的外部狀態,那么可以用相對較少的共享對象取代很多組對象
? 應用程序不依賴對象標識
享元模式特點:
? 享元模式的核心是把大量共享的對象收集在一起使用簡單工廠模式進行管理,避免由于大量的小對象導致系統內存過度消耗。
? 享元當重復對象較多時有很好的空間復雜度,但在查找搜索上消耗了時間復雜度。
轉載于:https://www.cnblogs.com/libingql/p/3635354.html
總結
以上是生活随笔為你收集整理的C#设计模式系列:享元模式(Flyweight)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 聚集索引和非聚集索引(整理)
- 下一篇: 用C#实现RSS的生成和解析,支持RSS