Flyweight 使用共享来有效地支持大量的细粒度对象

ContractedBlock.gif ExpandedBlockStart.gif IChemical
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.Flyweight
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public interface IChemical
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7ExpandedSubBlockStart.gifContractedSubBlock.gif        string Namedot.gif{get;}
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        string Symbol dot.gif{get;}
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        double AtomicWeightdot.gif{get;}
10ExpandedSubBlockEnd.gif    }

11ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif ChemicalFactory
 1None.gifusing System;
 2None.gifusing System.Collections;
 3None.gif
 4None.gifnamespace Gof.Test.Flyweight
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6InBlock.gif    public class ChemicalFactory
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        private static Hashtable _chemicals = new Hashtable();
 9InBlock.gif        private class ChemicalImpl:IChemical
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            public ChemicalImpl(string name,string symbol,double atomicweight)
12ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
13InBlock.gif                _name = name;
14InBlock.gif                _symbol = symbol;
15InBlock.gif                _atomicWeight = atomicweight;
16ExpandedSubBlockEnd.gif            }

17InBlock.gif            public string Name
18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
19InBlock.gif                get
20ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
21InBlock.gif                    return _name;
22ExpandedSubBlockEnd.gif                }

23InBlock.gif                set
24ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
25InBlock.gif                    _name = value;
26ExpandedSubBlockEnd.gif                }

27ExpandedSubBlockEnd.gif            }
private string _name = string.Empty;
28InBlock.gif            public string Symbol
29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
30InBlock.gif                get
31ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
32InBlock.gif                    return _symbol;
33ExpandedSubBlockEnd.gif                }

34InBlock.gif                set
35ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
36InBlock.gif                    _symbol = value;
37ExpandedSubBlockEnd.gif                }

38ExpandedSubBlockEnd.gif            }
private string _symbol = string.Empty;
39InBlock.gif            public double AtomicWeight
40ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
41InBlock.gif                get
42ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
43InBlock.gif                    return _atomicWeight;
44ExpandedSubBlockEnd.gif                }

45InBlock.gif                set
46ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
47InBlock.gif                    _atomicWeight = value;
48ExpandedSubBlockEnd.gif                }

49ExpandedSubBlockEnd.gif            }
private double _atomicWeight = 0;
50ExpandedSubBlockEnd.gif        }

51InBlock.gif        static ChemicalFactory()
52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
53InBlock.gif            _chemicals["carbon"= new ChemicalImpl("Carbon","c",12);
54InBlock.gif            _chemicals["sulfur"= new ChemicalImpl("sulfur","s",32);
55InBlock.gif            _chemicals["saltpeter"= new ChemicalImpl("saltpeter","KN03",101);
56InBlock.gif            //dot.gif
57ExpandedSubBlockEnd.gif        }

58InBlock.gif        public static IChemical GetChemical(string name)
59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
60InBlock.gif             return (IChemical)_chemicals[name.ToLower()];
61ExpandedSubBlockEnd.gif        }

62ExpandedSubBlockEnd.gif    }

63ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif Substance
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.Flyweight
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class Substance
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        private IChemical _chemical;
 8InBlock.gif        public Substance()
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
10InBlock.gif        public Substance(string name)
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12InBlock.gif            _chemical = ChemicalFactory.GetChemical(name);
13ExpandedSubBlockEnd.gif        }

14InBlock.gif        public Substance(string name,double grams):this(name)
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            _grams = grams;
17ExpandedSubBlockEnd.gif        }

18InBlock.gif        public double Grams
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20InBlock.gif            get
21ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
22InBlock.gif                return _grams;
23ExpandedSubBlockEnd.gif            }

24InBlock.gif            set
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                _grams = value;
27ExpandedSubBlockEnd.gif            }

28ExpandedSubBlockEnd.gif        }
private double _grams = 0;
29InBlock.gif        public string Name
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
31InBlock.gif            get
32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
33InBlock.gif                return _chemical.Name;
34ExpandedSubBlockEnd.gif            }

35ExpandedSubBlockEnd.gif        }

36InBlock.gif        public string Symbol
37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
38InBlock.gif            get
39ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
40InBlock.gif                return _chemical.Symbol;
41ExpandedSubBlockEnd.gif            }

42ExpandedSubBlockEnd.gif        }

43InBlock.gif        public double AtomicWeight
44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
45InBlock.gif            get
46ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
47InBlock.gif                return _chemical.AtomicWeight;
48ExpandedSubBlockEnd.gif            }

49ExpandedSubBlockEnd.gif        }

50InBlock.gif        public double Modles
51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
52InBlock.gif            get
53ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
54InBlock.gif                return _grams/AtomicWeight;
55ExpandedSubBlockEnd.gif            }

56ExpandedSubBlockEnd.gif        }

57ExpandedSubBlockEnd.gif    }

58ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif MIxture
  1None.gifusing System;
  2None.gifusing System.Collections;
  3None.gif
  4None.gifnamespace Gof.Test.Flyweight
  5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  6InBlock.gif    public class Mixture:IList
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  8InBlock.gif        public Mixture()
  9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
 10InBlock.gif
 11InBlock.gif        private ArrayList _list = new ArrayList();
 12InBlock.gif
 13ContractedSubBlock.gifExpandedSubBlockStart.gif        ICollection 成员#region ICollection 成员
 14InBlock.gif
 15InBlock.gif        public bool IsSynchronized
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 17InBlock.gif            get
 18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 19InBlock.gif                return _list.IsSynchronized;
 20ExpandedSubBlockEnd.gif            }

 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif
 23InBlock.gif        public int Count
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 25InBlock.gif            get
 26ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 27InBlock.gif                return _list.Count;
 28ExpandedSubBlockEnd.gif            }

 29ExpandedSubBlockEnd.gif        }

 30InBlock.gif
 31InBlock.gif        public void CopyTo(Array array, int index)
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 33InBlock.gif            _list.CopyTo(array,index);
 34ExpandedSubBlockEnd.gif        }

 35InBlock.gif
 36InBlock.gif        public object SyncRoot
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 38InBlock.gif            get
 39ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 40InBlock.gif                return _list.SyncRoot;
 41ExpandedSubBlockEnd.gif            }

 42ExpandedSubBlockEnd.gif        }

 43InBlock.gif
 44ExpandedSubBlockEnd.gif        #endregion

 45InBlock.gif
 46ContractedSubBlock.gifExpandedSubBlockStart.gif        IEnumerable 成员#region IEnumerable 成员
 47InBlock.gif
 48InBlock.gif        public IEnumerator GetEnumerator()
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 50InBlock.gif            return _list.GetEnumerator();
 51ExpandedSubBlockEnd.gif        }

 52InBlock.gif
 53ExpandedSubBlockEnd.gif        #endregion

 54InBlock.gif
 55ContractedSubBlock.gifExpandedSubBlockStart.gif        IList 成员#region IList 成员
 56InBlock.gif
 57InBlock.gif        public bool IsReadOnly
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 59InBlock.gif            get
 60ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 61InBlock.gif                return _list.IsReadOnly;
 62ExpandedSubBlockEnd.gif            }

 63ExpandedSubBlockEnd.gif        }

 64InBlock.gif
 65InBlock.gif        public object this[int index]
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 67InBlock.gif            get
 68ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 69InBlock.gif                return _list[index];
 70ExpandedSubBlockEnd.gif            }

 71InBlock.gif            set
 72ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 73InBlock.gif                _list[index] = value;
 74ExpandedSubBlockEnd.gif            }

 75ExpandedSubBlockEnd.gif        }

 76InBlock.gif
 77InBlock.gif        public void RemoveAt(int index)
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 79InBlock.gif            _list.RemoveAt(index);
 80ExpandedSubBlockEnd.gif        }

 81InBlock.gif
 82InBlock.gif        void IList.Insert(int index, object value)
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 84InBlock.gif            _list.Insert(index,value);
 85ExpandedSubBlockEnd.gif        }

 86InBlock.gif
 87InBlock.gif        public void Insert(int index, Substance value)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89InBlock.gif            ((IList)this).Insert(index,value);
 90ExpandedSubBlockEnd.gif        }

 91InBlock.gif
 92InBlock.gif        void IList.Remove(object value)
 93ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 94InBlock.gif            _list.Remove(value);
 95ExpandedSubBlockEnd.gif        }

 96InBlock.gif
 97InBlock.gif        public void Remove(Substance value)
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 99InBlock.gif            ((IList)this).Remove(value);
100ExpandedSubBlockEnd.gif        }

101InBlock.gif
102InBlock.gif        bool IList.Contains(object value)
103ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
104InBlock.gif            return _list.Contains(value);
105ExpandedSubBlockEnd.gif        }

106InBlock.gif
107InBlock.gif        public bool Contains(Substance value)
108ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
109InBlock.gif            return ((IList)this).Contains(value);
110ExpandedSubBlockEnd.gif        }

111InBlock.gif
112InBlock.gif        public void Clear()
113ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
114InBlock.gif            _list.Clear();
115ExpandedSubBlockEnd.gif        }

116InBlock.gif
117InBlock.gif        int IList.IndexOf(object value)
118ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
119InBlock.gif            return _list.IndexOf(value);
120ExpandedSubBlockEnd.gif        }

121InBlock.gif
122InBlock.gif        public int IndexOf(Substance value)
123ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
124InBlock.gif            return ((IList)this).IndexOf(value);
125ExpandedSubBlockEnd.gif        }

126InBlock.gif
127InBlock.gif        int IList.Add(object value)
128ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
129InBlock.gif            return _list.Add(value);
130ExpandedSubBlockEnd.gif        }

131InBlock.gif
132InBlock.gif        public int Add(Substance value)
133ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
134InBlock.gif            return ((IList)this).Add(value);
135ExpandedSubBlockEnd.gif        }

136InBlock.gif
137InBlock.gif        public bool IsFixedSize
138ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
139InBlock.gif            get
140ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
141InBlock.gif                return _list.IsFixedSize;
142ExpandedSubBlockEnd.gif            }

143ExpandedSubBlockEnd.gif        }

144InBlock.gif
145ExpandedSubBlockEnd.gif        #endregion

146InBlock.gif
147ContractedSubBlock.gifExpandedSubBlockStart.gif        Override Methods#region Override Methods
148InBlock.gif        public override string ToString()
149ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
150InBlock.gif            string results = string.Empty;
151InBlock.gif            for(int i = 0;i < _list.Count;i++)
152ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
153InBlock.gif                results += ((Substance)_list[i]).Name+" ";
154ExpandedSubBlockEnd.gif            }

155InBlock.gif            return results;
156ExpandedSubBlockEnd.gif        }

157InBlock.gif
158ExpandedSubBlockEnd.gif        #endregion

159ExpandedSubBlockEnd.gif    }

160ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 客户代码
1None.gif            Gof.Test.Flyweight.Mixture blackpowder = new Gof.Test.Flyweight.Mixture();
2None.gif            blackpowder.Add(new Substance("sulfur"));
3None.gif            blackpowder.Add(new Substance("saltpeter"));
4None.gif            blackpowder.Add(new Substance("carbon"));
5None.gif            Console.WriteLine( blackpowder.ToString() );
6None.gif            Console.ReadLine();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值