Flyweight 使用共享来有效地支持大量的细粒度对象
IChemical
1using System;
2
3namespace Gof.Test.Flyweight
4{
5 public interface IChemical
6 {
7 string Name{get;}
8 string Symbol {get;}
9 double AtomicWeight{get;}
10 }
11}
1using System;
2
3namespace Gof.Test.Flyweight
4{
5 public interface IChemical
6 {
7 string Name{get;}
8 string Symbol {get;}
9 double AtomicWeight{get;}
10 }
11}
ChemicalFactory
1using System;
2using System.Collections;
3
4namespace Gof.Test.Flyweight
5{
6 public class ChemicalFactory
7 {
8 private static Hashtable _chemicals = new Hashtable();
9 private class ChemicalImpl:IChemical
10 {
11 public ChemicalImpl(string name,string symbol,double atomicweight)
12 {
13 _name = name;
14 _symbol = symbol;
15 _atomicWeight = atomicweight;
16 }
17 public string Name
18 {
19 get
20 {
21 return _name;
22 }
23 set
24 {
25 _name = value;
26 }
27 }private string _name = string.Empty;
28 public string Symbol
29 {
30 get
31 {
32 return _symbol;
33 }
34 set
35 {
36 _symbol = value;
37 }
38 }private string _symbol = string.Empty;
39 public double AtomicWeight
40 {
41 get
42 {
43 return _atomicWeight;
44 }
45 set
46 {
47 _atomicWeight = value;
48 }
49 }private double _atomicWeight = 0;
50 }
51 static ChemicalFactory()
52 {
53 _chemicals["carbon"] = new ChemicalImpl("Carbon","c",12);
54 _chemicals["sulfur"] = new ChemicalImpl("sulfur","s",32);
55 _chemicals["saltpeter"] = new ChemicalImpl("saltpeter","KN03",101);
56 //
57 }
58 public static IChemical GetChemical(string name)
59 {
60 return (IChemical)_chemicals[name.ToLower()];
61 }
62 }
63}
1using System;
2using System.Collections;
3
4namespace Gof.Test.Flyweight
5{
6 public class ChemicalFactory
7 {
8 private static Hashtable _chemicals = new Hashtable();
9 private class ChemicalImpl:IChemical
10 {
11 public ChemicalImpl(string name,string symbol,double atomicweight)
12 {
13 _name = name;
14 _symbol = symbol;
15 _atomicWeight = atomicweight;
16 }
17 public string Name
18 {
19 get
20 {
21 return _name;
22 }
23 set
24 {
25 _name = value;
26 }
27 }private string _name = string.Empty;
28 public string Symbol
29 {
30 get
31 {
32 return _symbol;
33 }
34 set
35 {
36 _symbol = value;
37 }
38 }private string _symbol = string.Empty;
39 public double AtomicWeight
40 {
41 get
42 {
43 return _atomicWeight;
44 }
45 set
46 {
47 _atomicWeight = value;
48 }
49 }private double _atomicWeight = 0;
50 }
51 static ChemicalFactory()
52 {
53 _chemicals["carbon"] = new ChemicalImpl("Carbon","c",12);
54 _chemicals["sulfur"] = new ChemicalImpl("sulfur","s",32);
55 _chemicals["saltpeter"] = new ChemicalImpl("saltpeter","KN03",101);
56 //
57 }
58 public static IChemical GetChemical(string name)
59 {
60 return (IChemical)_chemicals[name.ToLower()];
61 }
62 }
63}
Substance
1using System;
2
3namespace Gof.Test.Flyweight
4{
5 public class Substance
6 {
7 private IChemical _chemical;
8 public Substance()
9 {}
10 public Substance(string name)
11 {
12 _chemical = ChemicalFactory.GetChemical(name);
13 }
14 public Substance(string name,double grams):this(name)
15 {
16 _grams = grams;
17 }
18 public double Grams
19 {
20 get
21 {
22 return _grams;
23 }
24 set
25 {
26 _grams = value;
27 }
28 }private double _grams = 0;
29 public string Name
30 {
31 get
32 {
33 return _chemical.Name;
34 }
35 }
36 public string Symbol
37 {
38 get
39 {
40 return _chemical.Symbol;
41 }
42 }
43 public double AtomicWeight
44 {
45 get
46 {
47 return _chemical.AtomicWeight;
48 }
49 }
50 public double Modles
51 {
52 get
53 {
54 return _grams/AtomicWeight;
55 }
56 }
57 }
58}
1using System;
2
3namespace Gof.Test.Flyweight
4{
5 public class Substance
6 {
7 private IChemical _chemical;
8 public Substance()
9 {}
10 public Substance(string name)
11 {
12 _chemical = ChemicalFactory.GetChemical(name);
13 }
14 public Substance(string name,double grams):this(name)
15 {
16 _grams = grams;
17 }
18 public double Grams
19 {
20 get
21 {
22 return _grams;
23 }
24 set
25 {
26 _grams = value;
27 }
28 }private double _grams = 0;
29 public string Name
30 {
31 get
32 {
33 return _chemical.Name;
34 }
35 }
36 public string Symbol
37 {
38 get
39 {
40 return _chemical.Symbol;
41 }
42 }
43 public double AtomicWeight
44 {
45 get
46 {
47 return _chemical.AtomicWeight;
48 }
49 }
50 public double Modles
51 {
52 get
53 {
54 return _grams/AtomicWeight;
55 }
56 }
57 }
58}
MIxture
1using System;
2using System.Collections;
3
4namespace Gof.Test.Flyweight
5{
6 public class Mixture:IList
7 {
8 public Mixture()
9 {}
10
11 private ArrayList _list = new ArrayList();
12
13 ICollection 成员#region ICollection 成员
14
15 public bool IsSynchronized
16 {
17 get
18 {
19 return _list.IsSynchronized;
20 }
21 }
22
23 public int Count
24 {
25 get
26 {
27 return _list.Count;
28 }
29 }
30
31 public void CopyTo(Array array, int index)
32 {
33 _list.CopyTo(array,index);
34 }
35
36 public object SyncRoot
37 {
38 get
39 {
40 return _list.SyncRoot;
41 }
42 }
43
44 #endregion
45
46 IEnumerable 成员#region IEnumerable 成员
47
48 public IEnumerator GetEnumerator()
49 {
50 return _list.GetEnumerator();
51 }
52
53 #endregion
54
55 IList 成员#region IList 成员
56
57 public bool IsReadOnly
58 {
59 get
60 {
61 return _list.IsReadOnly;
62 }
63 }
64
65 public object this[int index]
66 {
67 get
68 {
69 return _list[index];
70 }
71 set
72 {
73 _list[index] = value;
74 }
75 }
76
77 public void RemoveAt(int index)
78 {
79 _list.RemoveAt(index);
80 }
81
82 void IList.Insert(int index, object value)
83 {
84 _list.Insert(index,value);
85 }
86
87 public void Insert(int index, Substance value)
88 {
89 ((IList)this).Insert(index,value);
90 }
91
92 void IList.Remove(object value)
93 {
94 _list.Remove(value);
95 }
96
97 public void Remove(Substance value)
98 {
99 ((IList)this).Remove(value);
100 }
101
102 bool IList.Contains(object value)
103 {
104 return _list.Contains(value);
105 }
106
107 public bool Contains(Substance value)
108 {
109 return ((IList)this).Contains(value);
110 }
111
112 public void Clear()
113 {
114 _list.Clear();
115 }
116
117 int IList.IndexOf(object value)
118 {
119 return _list.IndexOf(value);
120 }
121
122 public int IndexOf(Substance value)
123 {
124 return ((IList)this).IndexOf(value);
125 }
126
127 int IList.Add(object value)
128 {
129 return _list.Add(value);
130 }
131
132 public int Add(Substance value)
133 {
134 return ((IList)this).Add(value);
135 }
136
137 public bool IsFixedSize
138 {
139 get
140 {
141 return _list.IsFixedSize;
142 }
143 }
144
145 #endregion
146
147 Override Methods#region Override Methods
148 public override string ToString()
149 {
150 string results = string.Empty;
151 for(int i = 0;i < _list.Count;i++)
152 {
153 results += ((Substance)_list[i]).Name+" ";
154 }
155 return results;
156 }
157
158 #endregion
159 }
160}
1using System;
2using System.Collections;
3
4namespace Gof.Test.Flyweight
5{
6 public class Mixture:IList
7 {
8 public Mixture()
9 {}
10
11 private ArrayList _list = new ArrayList();
12
13 ICollection 成员#region ICollection 成员
14
15 public bool IsSynchronized
16 {
17 get
18 {
19 return _list.IsSynchronized;
20 }
21 }
22
23 public int Count
24 {
25 get
26 {
27 return _list.Count;
28 }
29 }
30
31 public void CopyTo(Array array, int index)
32 {
33 _list.CopyTo(array,index);
34 }
35
36 public object SyncRoot
37 {
38 get
39 {
40 return _list.SyncRoot;
41 }
42 }
43
44 #endregion
45
46 IEnumerable 成员#region IEnumerable 成员
47
48 public IEnumerator GetEnumerator()
49 {
50 return _list.GetEnumerator();
51 }
52
53 #endregion
54
55 IList 成员#region IList 成员
56
57 public bool IsReadOnly
58 {
59 get
60 {
61 return _list.IsReadOnly;
62 }
63 }
64
65 public object this[int index]
66 {
67 get
68 {
69 return _list[index];
70 }
71 set
72 {
73 _list[index] = value;
74 }
75 }
76
77 public void RemoveAt(int index)
78 {
79 _list.RemoveAt(index);
80 }
81
82 void IList.Insert(int index, object value)
83 {
84 _list.Insert(index,value);
85 }
86
87 public void Insert(int index, Substance value)
88 {
89 ((IList)this).Insert(index,value);
90 }
91
92 void IList.Remove(object value)
93 {
94 _list.Remove(value);
95 }
96
97 public void Remove(Substance value)
98 {
99 ((IList)this).Remove(value);
100 }
101
102 bool IList.Contains(object value)
103 {
104 return _list.Contains(value);
105 }
106
107 public bool Contains(Substance value)
108 {
109 return ((IList)this).Contains(value);
110 }
111
112 public void Clear()
113 {
114 _list.Clear();
115 }
116
117 int IList.IndexOf(object value)
118 {
119 return _list.IndexOf(value);
120 }
121
122 public int IndexOf(Substance value)
123 {
124 return ((IList)this).IndexOf(value);
125 }
126
127 int IList.Add(object value)
128 {
129 return _list.Add(value);
130 }
131
132 public int Add(Substance value)
133 {
134 return ((IList)this).Add(value);
135 }
136
137 public bool IsFixedSize
138 {
139 get
140 {
141 return _list.IsFixedSize;
142 }
143 }
144
145 #endregion
146
147 Override Methods#region Override Methods
148 public override string ToString()
149 {
150 string results = string.Empty;
151 for(int i = 0;i < _list.Count;i++)
152 {
153 results += ((Substance)_list[i]).Name+" ";
154 }
155 return results;
156 }
157
158 #endregion
159 }
160}
客户代码
1 Gof.Test.Flyweight.Mixture blackpowder = new Gof.Test.Flyweight.Mixture();
2 blackpowder.Add(new Substance("sulfur"));
3 blackpowder.Add(new Substance("saltpeter"));
4 blackpowder.Add(new Substance("carbon"));
5 Console.WriteLine( blackpowder.ToString() );
6 Console.ReadLine();
1 Gof.Test.Flyweight.Mixture blackpowder = new Gof.Test.Flyweight.Mixture();
2 blackpowder.Add(new Substance("sulfur"));
3 blackpowder.Add(new Substance("saltpeter"));
4 blackpowder.Add(new Substance("carbon"));
5 Console.WriteLine( blackpowder.ToString() );
6 Console.ReadLine();