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

 IChemical
IChemical1
 using System;
using System;2

3
 namespace Gof.Test.Flyweight
namespace Gof.Test.Flyweight4


 {
{5
 public interface IChemical
    public interface IChemical6

 
     {
{7

 string Name
        string Name {get;}
{get;}8

 string Symbol
        string Symbol  {get;}
{get;}9

 double AtomicWeight
        double AtomicWeight {get;}
{get;}10
 }
    }11
 }
}
 ChemicalFactory
ChemicalFactory1
 using System;
using System;2
 using System.Collections;
using System.Collections;3

4
 namespace Gof.Test.Flyweight
namespace Gof.Test.Flyweight5


 {
{6
 public class ChemicalFactory
    public class ChemicalFactory7

 
     {
{8
 private static Hashtable _chemicals = new Hashtable();
        private static Hashtable _chemicals = new Hashtable();9
 private class ChemicalImpl:IChemical
        private class ChemicalImpl:IChemical10

 
         {
{11
 public ChemicalImpl(string name,string symbol,double atomicweight)
            public ChemicalImpl(string name,string symbol,double atomicweight)12

 
             {
{13
 _name = name;
                _name = name;14
 _symbol = symbol;
                _symbol = symbol;15
 _atomicWeight = atomicweight;
                _atomicWeight = atomicweight;16
 }
            }17
 public string Name
            public string Name18

 
             {
{19
 get
                get20

 
                 {
{21
 return _name;
                    return _name;22
 }
                }23
 set
                set24

 
                 {
{25
 _name = value;
                    _name = value;26
 }
                }27
 }private string _name = string.Empty;
            }private string _name = string.Empty;28
 public string Symbol
            public string Symbol29

 
             {
{30
 get
                get31

 
                 {
{32
 return _symbol;
                    return _symbol;33
 }
                }34
 set
                set35

 
                 {
{36
 _symbol = value;
                    _symbol = value;37
 }
                }38
 }private string _symbol = string.Empty;
            }private string _symbol = string.Empty;39
 public double AtomicWeight
            public double AtomicWeight40

 
             {
{41
 get
                get42

 
                 {
{43
 return _atomicWeight;
                    return _atomicWeight;44
 }
                }45
 set
                set46

 
                 {
{47
 _atomicWeight = value;
                    _atomicWeight = value;48
 }
                }49
 }private double _atomicWeight = 0;
            }private double _atomicWeight = 0;50
 }
        }51
 static ChemicalFactory()
        static ChemicalFactory()52

 
         {
{53
 _chemicals["carbon"] = new ChemicalImpl("Carbon","c",12);
            _chemicals["carbon"] = new ChemicalImpl("Carbon","c",12);54
 _chemicals["sulfur"] = new ChemicalImpl("sulfur","s",32);
            _chemicals["sulfur"] = new ChemicalImpl("sulfur","s",32);55
 _chemicals["saltpeter"] = new ChemicalImpl("saltpeter","KN03",101);
            _chemicals["saltpeter"] = new ChemicalImpl("saltpeter","KN03",101);56
 //
            //
57
 }
        }58
 public static IChemical GetChemical(string name)
        public static IChemical GetChemical(string name)59

 
         {
{60
 return (IChemical)_chemicals[name.ToLower()];
             return (IChemical)_chemicals[name.ToLower()];61
 }
        }62
 }
    }63
 }
}
 Substance
Substance1
 using System;
using System;2

3
 namespace Gof.Test.Flyweight
namespace Gof.Test.Flyweight4


 {
{5
 public class Substance
    public class Substance6

 
     {
{7
 private IChemical _chemical;
        private IChemical _chemical;8
 public Substance()
        public Substance()9

 
         {}
{}10
 public Substance(string name)
        public Substance(string name)11

 
         {
{12
 _chemical = ChemicalFactory.GetChemical(name);
            _chemical = ChemicalFactory.GetChemical(name);13
 }
        }14
 public Substance(string name,double grams):this(name)
        public Substance(string name,double grams):this(name)15

 
         {
{16
 _grams = grams;
            _grams = grams;17
 }
        }18
 public double Grams
        public double Grams19

 
         {
{20
 get
            get21

 
             {
{22
 return _grams;
                return _grams;23
 }
            }24
 set
            set25

 
             {
{26
 _grams = value;
                _grams = value;27
 }
            }28
 }private double _grams = 0;
        }private double _grams = 0;29
 public string Name
        public string Name30

 
         {
{31
 get
            get32

 
             {
{33
 return _chemical.Name;
                return _chemical.Name;34
 }
            }35
 }
        }36
 public string Symbol
        public string Symbol37

 
         {
{38
 get
            get39

 
             {
{40
 return _chemical.Symbol;
                return _chemical.Symbol;41
 }
            }42
 }
        }43
 public double AtomicWeight
        public double AtomicWeight44

 
         {
{45
 get
            get46

 
             {
{47
 return _chemical.AtomicWeight;
                return _chemical.AtomicWeight;48
 }
            }49
 }
        }50
 public double Modles
        public double Modles51

 
         {
{52
 get
            get53

 
             {
{54
 return _grams/AtomicWeight;
                return _grams/AtomicWeight;55
 }
            }56
 }
        }57
 }
    }58
 }
}
 MIxture
MIxture1
 using System;
using System;2
 using System.Collections;
using System.Collections;3

4
 namespace Gof.Test.Flyweight
namespace Gof.Test.Flyweight5


 {
{6
 public class Mixture:IList
    public class Mixture:IList7

 
     {
{8
 public Mixture()
        public Mixture()9

 
         {}
{}10

11
 private ArrayList _list = new ArrayList();
        private ArrayList _list = new ArrayList();12

13

 ICollection 成员#region ICollection 成员
        ICollection 成员#region ICollection 成员14

15
 public bool IsSynchronized
        public bool IsSynchronized16

 
         {
{17
 get
            get18

 
             {
{19
 return _list.IsSynchronized;
                return _list.IsSynchronized;20
 }
            }21
 }
        }22

23
 public int Count
        public int Count24

 
         {
{25
 get
            get26

 
             {
{27
 return _list.Count;
                return _list.Count;28
 }
            }29
 }
        }30

31
 public void CopyTo(Array array, int index)
        public void CopyTo(Array array, int index)32

 
         {
{33
 _list.CopyTo(array,index);
            _list.CopyTo(array,index);34
 }
        }35

36
 public object SyncRoot
        public object SyncRoot37

 
         {
{38
 get
            get39

 
             {
{40
 return _list.SyncRoot;
                return _list.SyncRoot;41
 }
            }42
 }
        }43

44
 #endregion
        #endregion45

46

 IEnumerable 成员#region IEnumerable 成员
        IEnumerable 成员#region IEnumerable 成员47

48
 public IEnumerator GetEnumerator()
        public IEnumerator GetEnumerator()49

 
         {
{50
 return _list.GetEnumerator();
            return _list.GetEnumerator();51
 }
        }52

53
 #endregion
        #endregion54

55

 IList 成员#region IList 成员
        IList 成员#region IList 成员56

57
 public bool IsReadOnly
        public bool IsReadOnly58

 
         {
{59
 get
            get60

 
             {
{61
 return _list.IsReadOnly;
                return _list.IsReadOnly;62
 }
            }63
 }
        }64

65
 public object this[int index]
        public object this[int index]66

 
         {
{67
 get
            get68

 
             {
{69
 return _list[index];
                return _list[index];70
 }
            }71
 set
            set72

 
             {
{73
 _list[index] = value;
                _list[index] = value;74
 }
            }75
 }
        }76

77
 public void RemoveAt(int index)
        public void RemoveAt(int index)78

 
         {
{79
 _list.RemoveAt(index);
            _list.RemoveAt(index);80
 }
        }81

82
 void IList.Insert(int index, object value)
        void IList.Insert(int index, object value)83

 
         {
{84
 _list.Insert(index,value);
            _list.Insert(index,value);85
 }
        }86

87
 public void Insert(int index, Substance value)
        public void Insert(int index, Substance value)88

 
         {
{89
 ((IList)this).Insert(index,value);
            ((IList)this).Insert(index,value);90
 }
        }91

92
 void IList.Remove(object value)
        void IList.Remove(object value)93

 
         {
{94
 _list.Remove(value);
            _list.Remove(value);95
 }
        }96

97
 public void Remove(Substance value)
        public void Remove(Substance value)98

 
         {
{99
 ((IList)this).Remove(value);
            ((IList)this).Remove(value);100
 }
        }101

102
 bool IList.Contains(object value)
        bool IList.Contains(object value)103

 
         {
{104
 return _list.Contains(value);
            return _list.Contains(value);105
 }
        }106

107
 public bool Contains(Substance value)
        public bool Contains(Substance value)108

 
         {
{109
 return ((IList)this).Contains(value);
            return ((IList)this).Contains(value);110
 }
        }111

112
 public void Clear()
        public void Clear()113

 
         {
{114
 _list.Clear();
            _list.Clear();115
 }
        }116

117
 int IList.IndexOf(object value)
        int IList.IndexOf(object value)118

 
         {
{119
 return _list.IndexOf(value);
            return _list.IndexOf(value);120
 }
        }121

122
 public int IndexOf(Substance value)
        public int IndexOf(Substance value)123

 
         {
{124
 return ((IList)this).IndexOf(value);
            return ((IList)this).IndexOf(value);125
 }
        }126

127
 int IList.Add(object value)
        int IList.Add(object value)128

 
         {
{129
 return _list.Add(value);
            return _list.Add(value);130
 }
        }131

132
 public int Add(Substance value)
        public int Add(Substance value)133

 
         {
{134
 return ((IList)this).Add(value);
            return ((IList)this).Add(value);135
 }
        }136

137
 public bool IsFixedSize
        public bool IsFixedSize138

 
         {
{139
 get
            get140

 
             {
{141
 return _list.IsFixedSize;
                return _list.IsFixedSize;142
 }
            }143
 }
        }144

145
 #endregion
        #endregion146

147

 Override Methods#region Override Methods
        Override Methods#region Override Methods148
 public override string ToString()
        public override string ToString()149

 
         {
{150
 string results = string.Empty;
            string results = string.Empty;151
 for(int i = 0;i < _list.Count;i++)
            for(int i = 0;i < _list.Count;i++)152

 
             {
{153
 results += ((Substance)_list[i]).Name+" ";
                results += ((Substance)_list[i]).Name+" ";154
 }
            }155
 return results;
            return results;156
 }
        }157

158
 #endregion
        #endregion159
 }
    }160
 }
}
 客户代码
客户代码1
 Gof.Test.Flyweight.Mixture blackpowder = new Gof.Test.Flyweight.Mixture();
            Gof.Test.Flyweight.Mixture blackpowder = new Gof.Test.Flyweight.Mixture();2
 blackpowder.Add(new Substance("sulfur"));
            blackpowder.Add(new Substance("sulfur"));3
 blackpowder.Add(new Substance("saltpeter"));
            blackpowder.Add(new Substance("saltpeter"));4
 blackpowder.Add(new Substance("carbon"));
            blackpowder.Add(new Substance("carbon"));5
 Console.WriteLine( blackpowder.ToString() );
            Console.WriteLine( blackpowder.ToString() );6
 Console.ReadLine();
            Console.ReadLine();.jpg) 
  
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号