享元模式(Flyweight Pattern):運用共享技術有效地支持大量細粒度對象的複用。系統只使用少許的對象,而這些對象都很類似,狀態變化很小,能夠實現對象的屢次複用。因爲享元模式要求可以共享的對象必須是細粒度對象,所以它又稱爲輕量級模式,它是一種對象結構型模式。git
Flyweight類:github
namespace FlyweightPattern.BasicStructure { abstract class Flyweight { public abstract void Operation(int extrinsicState); } }
ConcreteFlyweight類:web
namespace FlyweightPattern.BasicStructure { class ConcreteFlyweight : Flyweight { public override void Operation(int extrinsicState) { Console.WriteLine("具體Flyweight:" + extrinsicState); } } }
UnSharedConcreteFlyweight類:編程
namespace FlyweightPattern.BasicStructure { class UnSharedConcreteFlyweight : Flyweight { public override void Operation(int extrinsicState) { Console.WriteLine("不共享的具體Flyweight:" + extrinsicState); } } }
FlyweightFactory類:ide
namespace FlyweightPattern.BasicStructure { 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]; } } }
客戶端調用代碼:網站
static void Main(string[] args) { try { {//BasicStructure int extrinsicState = 22; FlyweightFactory flyweightFactory = new FlyweightFactory(); var flyweight = flyweightFactory.GetFlyweight("X"); flyweight.Operation(--extrinsicState); flyweight = flyweightFactory.GetFlyweight("Y"); flyweight.Operation(--extrinsicState); flyweight = flyweightFactory.GetFlyweight("Z"); flyweight.Operation(--extrinsicState); UnSharedConcreteFlyweight uf = new UnSharedConcreteFlyweight(); uf.Operation(--extrinsicState); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); }
結果以下:this
場景模擬:因爲多個客戶須要作網站,其中有一些客戶的網站是基本同樣的,只有數據不同。spa
WebSite(網站抽象)類——Flyweight類code
namespace FlyweightPattern.SituationSimulation { /// <summary> /// 網站抽象類 /// </summary> abstract class WebSite { public abstract void Use(); } }
ConcreteWebSite(具體網站)類——ConcreteFlyweight類對象
namespace FlyweightPattern.SituationSimulation { /// <summary> /// 具體網站類 /// </summary> class ConcreteWebSite : WebSite { public string Name { get; } public ConcreteWebSite(string name) { this.Name = name; } public override void Use() { Console.WriteLine("網站分類:" + Name); } } }
WebSiteFactory(網站工廠)類——FlyweightFactory類
namespace FlyweightPattern.SituationSimulation { class WebSiteFactory { private Hashtable flyweights = new Hashtable(); public WebSite GetWebSiteCategory(string key) { if (!flyweights.ContainsKey(key)) flyweights.Add(key, new ConcreteWebSite(key)); return (WebSite)flyweights[key]; } public int GetWebSiteCount() { return flyweights.Count; } } }
客戶端調用代碼:
static void Main(string[] args) { try { {//SituationSimulation WebSiteFactory webSiteFactory = new WebSiteFactory(); var fx = webSiteFactory.GetWebSiteCategory("產品展現"); fx.Use(); var fy = webSiteFactory.GetWebSiteCategory("產品展現"); fy.Use(); var fz = webSiteFactory.GetWebSiteCategory("產品展現"); fz.Use(); var fl = webSiteFactory.GetWebSiteCategory("博客"); fl.Use(); var fm = webSiteFactory.GetWebSiteCategory("博客"); fm.Use(); var fn = webSiteFactory.GetWebSiteCategory("博客"); fn.Use(); Console.WriteLine($"網站分類總數爲:{webSiteFactory.GetWebSiteCount()}"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); }
結果以下:
享元模式的優勢
享元模式的缺點
在如下狀況下可使用享元模式: