日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#设计模式系列:享元模式(Flyweight)

發(fā)布時間:2025/3/19 C# 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#设计模式系列:享元模式(Flyweight) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  當(dāng)頻繁地從數(shù)據(jù)源讀取數(shù)據(jù)時,讀出的內(nèi)容存在重復(fù),那么需要使用享元模式(Flyweight)來提高內(nèi)存效率,Flyweight模式將節(jié)省更多空間,共享的Flyweight越多,空間節(jié)省越大。

1、享元模式簡介

1.1>、定義

  享元模式(Flyweight)的存在是為了避免大量擁有相同內(nèi)容的小類的開銷(如內(nèi)存開銷),使大家共享一個類。

1.2>、使用頻率

  ? 低

2、享元模式結(jié)構(gòu)

2.1>、結(jié)構(gòu)圖

?

2.2>、參與者

  享元模式參與者:

  ? Flyweight:聲明一個接口,通過這個接口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請求一個flyweight對象時,FlyweightFactory需要可以進(jìn)行分配,若flyweight對象不存在時,則先創(chuàng)建一個。

  ? Client:維持一個對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)用分析

  享元模式適用情形:

  ? 一個應(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ù)對象較多時有很好的空間復(fù)雜度,但在查找搜索上消耗了時間復(fù)雜度。

轉(zhuǎn)載于:https://www.cnblogs.com/libingql/p/3635354.html

總結(jié)

以上是生活随笔為你收集整理的C#设计模式系列:享元模式(Flyweight)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。