![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
IChemical
1
using System;
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
namespace Gof.Test.Flyweight
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
5
public interface IChemical
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string Name
{get;}
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string Symbol
{get;}
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
double AtomicWeight
{get;}
10
}
11
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ChemicalFactory
1
using System;
2
using System.Collections;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
namespace Gof.Test.Flyweight
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6
public class ChemicalFactory
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
private static Hashtable _chemicals = new Hashtable();
9
private class ChemicalImpl:IChemical
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
public ChemicalImpl(string name,string symbol,double atomicweight)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
_name = name;
14
_symbol = symbol;
15
_atomicWeight = atomicweight;
16
}
17
public string Name
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
get
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
return _name;
22
}
23
set
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
_name = value;
26
}
27
}private string _name = string.Empty;
28
public string Symbol
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
get
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
return _symbol;
33
}
34
set
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
_symbol = value;
37
}
38
}private string _symbol = string.Empty;
39
public double AtomicWeight
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41
get
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
return _atomicWeight;
44
}
45
set
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
_atomicWeight = value;
48
}
49
}private double _atomicWeight = 0;
50
}
51
static ChemicalFactory()
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
//![](https://www.cnblogs.com/Images/dot.gif)
57
}
58
public static IChemical GetChemical(string name)
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
return (IChemical)_chemicals[name.ToLower()];
61
}
62
}
63
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Substance
1
using System;
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
namespace Gof.Test.Flyweight
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
5
public class Substance
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
private IChemical _chemical;
8
public Substance()
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{}
10
public Substance(string name)
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
_chemical = ChemicalFactory.GetChemical(name);
13
}
14
public Substance(string name,double grams):this(name)
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
_grams = grams;
17
}
18
public double Grams
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
get
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
return _grams;
23
}
24
set
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
_grams = value;
27
}
28
}private double _grams = 0;
29
public string Name
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
get
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
return _chemical.Name;
34
}
35
}
36
public string Symbol
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
get
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
return _chemical.Symbol;
41
}
42
}
43
public double AtomicWeight
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
get
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
return _chemical.AtomicWeight;
48
}
49
}
50
public double Modles
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
get
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
return _grams/AtomicWeight;
55
}
56
}
57
}
58
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
MIxture
1
using System;
2
using System.Collections;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
namespace Gof.Test.Flyweight
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6
public class Mixture:IList
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
public Mixture()
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{}
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
private ArrayList _list = new ArrayList();
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ICollection 成员#region ICollection 成员
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
public bool IsSynchronized
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
get
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
return _list.IsSynchronized;
20
}
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
public int Count
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
get
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27
return _list.Count;
28
}
29
}
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
public void CopyTo(Array array, int index)
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
_list.CopyTo(array,index);
34
}
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
public object SyncRoot
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
get
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
return _list.SyncRoot;
41
}
42
}
43![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
#endregion
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IEnumerable 成员#region IEnumerable 成员
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
public IEnumerator GetEnumerator()
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
return _list.GetEnumerator();
51
}
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
#endregion
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IList 成员#region IList 成员
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
public bool IsReadOnly
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59
get
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
return _list.IsReadOnly;
62
}
63
}
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
public object this[int index]
66![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
67
get
68![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
return _list[index];
70
}
71
set
72![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
_list[index] = value;
74
}
75
}
76![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
public void RemoveAt(int index)
78![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79
_list.RemoveAt(index);
80
}
81![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
void IList.Insert(int index, object value)
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84
_list.Insert(index,value);
85
}
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
public void Insert(int index, Substance value)
88![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
89
((IList)this).Insert(index,value);
90
}
91![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
void IList.Remove(object value)
93![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
94
_list.Remove(value);
95
}
96![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
public void Remove(Substance value)
98![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
99
((IList)this).Remove(value);
100
}
101![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
bool IList.Contains(object value)
103![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
104
return _list.Contains(value);
105
}
106![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
public bool Contains(Substance value)
108![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
109
return ((IList)this).Contains(value);
110
}
111![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
112
public void Clear()
113![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
114
_list.Clear();
115
}
116![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117
int IList.IndexOf(object value)
118![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
119
return _list.IndexOf(value);
120
}
121![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
122
public int IndexOf(Substance value)
123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
return ((IList)this).IndexOf(value);
125
}
126![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127
int IList.Add(object value)
128![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
129
return _list.Add(value);
130
}
131![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
132
public int Add(Substance value)
133![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
134
return ((IList)this).Add(value);
135
}
136![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
137
public bool IsFixedSize
138![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
139
get
140![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
141
return _list.IsFixedSize;
142
}
143
}
144![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
145
#endregion
146![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
147![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Override Methods#region Override Methods
148
public override string ToString()
149![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
150
string results = string.Empty;
151
for(int i = 0;i < _list.Count;i++)
152![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
153
results += ((Substance)_list[i]).Name+" ";
154
}
155
return results;
156
}
157![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
158
#endregion
159
}
160
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
客户代码
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();
posted @
2007-01-08 12:40
南守拥
阅读(
261)
评论()
编辑
收藏
举报