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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Flyweight Pattern简单随笔

發(fā)布時間:2025/3/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Flyweight Pattern简单随笔 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

????? ?

?享元模式: 以共享的方式高效地支持大量的細粒度對象。

?享元對象的狀態(tài):
?
? 1:內蘊狀態(tài)(Internal State)內蘊狀態(tài)存儲在享元對象內部且不會隨環(huán)境改變而改變。因此內蘊狀態(tài)并可以共享。

? 2:外蘊狀態(tài)(External State)。外蘊狀態(tài)是隨環(huán)境改變而改變的、不可以共享的狀態(tài)。享元對象的外蘊狀態(tài)必須由客戶端保存,并在享元對象被創(chuàng)建之后,在需要使用的時候再傳入到享元對象內部。外蘊狀態(tài)與內蘊狀態(tài)是相互獨立的。

? 享元模式的應用條件:

? 1: 一個系統(tǒng)有大量的對象。

? 2:這些對象耗費大量的內存。

? 3:這些對象的狀態(tài)中的大部分都可以外部化。

? 4:這些對象可以按照內蘊狀態(tài)分成很多的組,當把外蘊對象從對象中剔除時,每一個組都可以僅用一個對象代替。

? 5:軟件系統(tǒng)不依賴于這些對象的身份,換言之,這些對象可以是不可分辨的。

? .NET的享元模式:
?
? .NET中的String類型就是運用了享元模式。.NET中如果第一次創(chuàng)建了一個字符串對象s1,下次再創(chuàng)建相同的字符串s2時只是把它的引用指向s1所引用的具體對象,這就實現(xiàn)了相同字符串在內存中的共享。

??

?? 在某些情況下,對象的數(shù)量可能會太多,從而導致了運行時的代價。那么我們如何去避免大量細粒度的對象,同時又不影響客戶程序使用面向對象的方式進行操作,運用共享技術有效地支持大量細粒度的對象。

?

私有屬性是元素特有的,可以通過傳遞參數(shù)來進行賦值,而公共屬性則通過繼承來賦予相應的值:

?

?1?using?System;
?2?using?System.Collections.Generic;
?3?using?System.Text;
?4?
?5?namespace?FlyWeightPatternSam
?6?{
?7?????public?abstract?class?Employer
?8?????{
?9?????????protected?string?_company?=?"COMPANY";//公共屬性
10?????????protected?string?_companyAddress?=?"ADDRESS";//公共屬性
11?
12?????????public?abstract?void?SetPropertity(string?name,string?age);
13?????????public?abstract?void?ShowInformation();
14?
15?????}
16?}
17?
18?using?System;
19?using?System.Collections.Generic;
20?using?System.Text;
21?
22?namespace?FlyWeightPatternSam
23?{
24?????public?class?EmployerA:Employer?
25?????{
26?????????private?string?_name;//特有屬性
27?????????private?string?_age;
28?????????private?string?_id="01";
29?????????public?override?void?SetPropertity(?string?name,?string?age)
30?????????{//傳遞相應的參數(shù)來賦值
31?????????????//throw?new?Exception("The?method?or?operation?is?not?implemented.");
32?????????????this._age?=?age;
33????????????
34?????????????this._name?=?name;
35?????????}
36?
37?????????public?override?void?ShowInformation()
38?????????{
39?????????????//throw?new?Exception("The?method?or?operation?is?not?implemented.");
40?????????????Console.WriteLine(_company?+"??"+_companyAddress?+"??"+_name?+"??"+_id?+"???"+_age?);
41?????????}
42?????}
43?}
44?

?

?

主要的是通過工廠來進行對象創(chuàng)建,當存在該對象時就取出該對象,否則拋出異常。同時,對工廠使用單件模式控制唯一性;

?

?1?using?System;
?2?using?System.Collections.Generic;
?3?using?System.Text;
?4?
?5?namespace?FlyWeightPatternSam
?6?{
?7?????public?class?FlyweightFactory
?8?????{
?9?????????private?static?readonly?FlyweightFactory?instance=new?FlyweightFactory();
10?????????System.Collections.Generic.Dictionary<string,?Employer>?dictionary?=?new?Dictionary<string,?Employer>();
11?
12?????????private?FlyweightFactory()?
13?????????{
14?????????????dictionary.Add("01",new?EmployerA());
15?????????????dictionary.Add("02",new?EmployerB());
16?????????????dictionary.Add("03",new?EmployerC());
17?????????}
18?????????
19?????????public?static??FlyweightFactory?Instance
20?????????{
21?????????????get
22?????????????{
23?????????????????return?instance;
24?????????????}
25?????????}
26?
27?????????public?Employer?GetEmployer(string?str)?
28?????????{
29?????????????Employer?employer?=?null?;
30?????????????try
31?????????????{
32?????????????????employer?=?dictionary[str]?as?Employer;????
33?????????????}
34?????????????catch(Exception?ex)
35?????????????{
36?????????????????Console.WriteLine(ex.Message);
37?????????????}
38?????????????return?employer;
39?????????}
40?????}
41?}
42?

?

?

客戶端代碼如下所示:

?

代碼 using?System;
using?System.Collections.Generic;
using?System.Text;

namespace?FlyWeightPatternSam
{
????
class?Program
????{
????????
static?void?Main(string[]?args)
????????{

????????????Employer?employer?
=?FlyweightFactory.Instance.GetEmployer("01");
????????????employer.SetPropertity(
"jasen","24");
????????????employer.ShowInformation();
????????????Console.ReadLine();
????????????
????????????employer?
=?FlyweightFactory.Instance.GetEmployer("04");
????????????Console.ReadLine();
????????????
????????????employer?
=?FlyweightFactory.Instance.GetEmployer("01");
????????????employer.SetPropertity(
"jasen",?"25");
????????????employer.ShowInformation();
????????????Console.ReadLine();
????????}
????}
}

?

?

操作結果如下:

?

**************************************************************************

源代碼下載:/Files/jasenkin/FlyWeightPatternSam.rar

?

轉載于:https://www.cnblogs.com/jasenkin/archive/2010/03/17/1688249.html

總結

以上是生活随笔為你收集整理的Flyweight Pattern简单随笔的全部內容,希望文章能夠幫你解決所遇到的問題。

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