Flyweight享元設計模式是一種結構型設計模式,它主要解決的問題是:由于(同類)對象的數(shù)量太大,采用面向對象時給系統(tǒng)帶來了難以承受的內存開銷。比如有這樣一個場景:一個停車場中有1000輛汽車,我們所定義的汽車對象占用內存 GoF《設計模式》中說道:運用共享技術有效的支持大量細粒度的對象。 Flyweight模式的結構大概是這樣的:
來看看程序,定義一個場景:有一個汽車類型,客戶程序要實例化1000個,實例化后查看一下內存分配情況。 普通的面向對象方式: class Class1 { [STAThread] static void { Console.WriteLine("實例化前:" + GC.GetTotalMemory(false)); ArrayList list = new ArrayList(1000); for(int i = 0;i < 1000;i++) { Car car = new Car(" list.Add(car); } Console.WriteLine("實例化后:" + GC.GetTotalMemory(false)); Console.Read(); } } public class Car { private string body; private string wheel; private string engine; private string brand; private string color; public string Body { get{return body;} set{body = value;} } public string Wheel { get{return wheel;} set{wheel = value;} } public string Engine { get{return engine;} set{engine = value;} } public string Brand { get{return brand;} set{brand = value;} } public string Color { get{return color;} set{color = value;} } public Car(string body,string wheel,string engine,string brand,string color) { Body = body; Wheel = wheel; Engine = engine; Brand = brand; Color = color; } } 內存分配情況如下: 實例化前:16384 實例化后:65536 然后再用Flyweight模式方式程序做一下比較: class Class1 { [STAThread] static void { Console.WriteLine("實例化前:" + GC.GetTotalMemory(false)); ArrayList list = new ArrayList(1000); for(int i = 0;i < 1000;i++) { FlyWeightCar car = FlyWeightFactory.CreateInit(" list.Add(car); } Console.WriteLine("實例化后:" + GC.GetTotalMemory(false)); Console.Read(); } } public class FlyWeightFactory { private static FlyWeightCar car; private static Hashtable table = new Hashtable(); public static FlyWeightCar CreateInit(string body,string wheel,string engine,string brand,string color) { if(table[brand] != null) { car = (FlyWeightCar)table[brand]; } else { car = new FlyWeightCar(); car.Brand = brand; car.CarBody = new CarBody(body,wheel,engine,color); table.Add(brand,car); } return car; } } public class FlyWeightCar { private string brand; public string Brand { get { return brand; } set { brand = value; } } private CarBody carbody; public CarBody CarBody { get { return carbody; } set { this.carbody = value; } } } public class CarBody { private string body; private string wheel; private string engine; private string color; public string Body { get{return body;} set{body = value;} } public string Wheel { get{return wheel;} set{wheel = value;} } public string Engine { get{return engine;} set{engine = value;} } public string Color { get{return color;} set{color = value;} } public CarBody(string body,string wheel,string engine,string color) { Body = body; Wheel = wheel; Engine = engine; Color = color; } } 內存分配情況: 實例化前:16384 實例化后:40960 從數(shù)字上不難看出內存分配的容量節(jié)省了不少,而且隨著數(shù)量的增加,差距會更大,當然我也測試了一下數(shù)量減少的情況,當我實例化100個對象是結果是普通方式的內存分配更小一些,所以,在使用時,我們一定要對所實例化對象的個數(shù)進行評估,否則的話會適得其反。 Flyweight模式的幾個要點: 1、面向對象很好的解決了抽象性的問題,但是作為一個運行在機器中的程序實體,我們需要考慮對象的代價問題。Flyweight設計模式主要解決面向對象的代價問題,一般不觸及面向對象的抽象性問題。 2、Flyweight采用對象共享的做法來降低系統(tǒng)中對象的個數(shù),從而降低細粒度對象給系統(tǒng)帶來的內存壓力。在具體實現(xiàn)方面,要注意對象的狀態(tài)處理。 3、對象的數(shù)量太大從而導致對象內存開銷加大(這個數(shù)量要經(jīng)過評估,而不能憑空臆斷) |
|