C#设计模式系列:享元模式(Flyweight)
當(dāng)頻繁地從數(shù)據(jù)源讀取數(shù)據(jù)時(shí),讀出的內(nèi)容存在重復(fù),那么需要使用享元模式(Flyweight)來提高內(nèi)存效率,Flyweight模式將節(jié)省更多空間,共享的Flyweight越多,空間節(jié)省越大。
1、享元模式簡介
1.1>、定義
享元模式(Flyweight)的存在是為了避免大量擁有相同內(nèi)容的小類的開銷(如內(nèi)存開銷),使大家共享一個(gè)類。
1.2>、使用頻率
? 低
2、享元模式結(jié)構(gòu)
2.1>、結(jié)構(gòu)圖
?
2.2>、參與者
享元模式參與者:
? Flyweight:聲明一個(gè)接口,通過這個(gè)接口flyweight可以直接接收并作用于外部狀態(tài)。
? ConcreteFlyweight:實(shí)現(xiàn)Flyweight接口,并為內(nèi)部狀態(tài)增加存儲空間。ConcreteFlyweight對象必須是可以共享的,它所存儲的狀態(tài)必須是內(nèi)部的,即它必須獨(dú)立于ConcreteFlyweight對象的場景。
? UnsharedConcreteFlyweight:并非所有的Flyweight子類都需要被共享。Flyweight接口使共享成為可能,但它不強(qiáng)制共享。在Flyweight對象結(jié)構(gòu)的某些層次,UnsharedConcreteFlyweight對象通常將ConcreteFlyweight對象作為子節(jié)點(diǎn)。
? FlyweightFactory
° 創(chuàng)建和管理flyweight對象。
° 確保flyweight對象被合理共享。當(dāng)Client請求一個(gè)flyweight對象時(shí),FlyweightFactory需要可以進(jìn)行分配,若flyweight對象不存在時(shí),則先創(chuàng)建一個(gè)。
? Client:維持一個(gè)對Flyweight的引用
在享元模式中,Client調(diào)用Flyweight下的ConcreteFlyweight,如果ConcreteFlyweight存在則調(diào)用成功;否則就調(diào)用FlyweightFactory生產(chǎn)所需要的繼承Flyweight接口的ConcreteFlyweight,以供調(diào)用。
3、享元模式結(jié)構(gòu)實(shí)現(xiàn)
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);}} }運(yùn)行輸出:
ConcreteFlyweight: 21 ConcreteFlyweight: 20 ConcreteFlyweight: 19 UnsharedConcreteFlyweight: 18 請按任意鍵繼續(xù). . .4、享元模式應(yīng)用分析
享元模式適用情形:
? 一個(gè)應(yīng)用程序使用了大量的對象
? 完全由于使用大量的對象,造成很大的存儲開銷
? 對象的大多數(shù)狀態(tài)都可變?yōu)橥獠繝顟B(tài)
? 如果刪除對象的外部狀態(tài),那么可以用相對較少的共享對象取代很多組對象
? 應(yīng)用程序不依賴對象標(biāo)識
享元模式特點(diǎn):
? 享元模式的核心是把大量共享的對象收集在一起使用簡單工廠模式進(jìn)行管理,避免由于大量的小對象導(dǎo)致系統(tǒng)內(nèi)存過度消耗。
? 享元當(dāng)重復(fù)對象較多時(shí)有很好的空間復(fù)雜度,但在查找搜索上消耗了時(shí)間復(fù)雜度。
轉(zhuǎn)載于:https://www.cnblogs.com/libingql/p/3635354.html
總結(jié)
以上是生活随笔為你收集整理的C#设计模式系列:享元模式(Flyweight)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 聚集索引和非聚集索引(整理)
- 下一篇: 对float的理解