|
5 | 5 |
|
6 | 6 | namespace IteratorPattern
|
7 | 7 | {
|
8 |
| -class RadioStation |
9 |
| -{ |
10 |
| - private float mFrequency; |
11 |
| - |
12 |
| - public RadioStation(float frequency) |
13 |
| - { |
14 |
| - mFrequency = frequency; |
15 |
| - } |
16 |
| - |
17 |
| - public float GetFrequecy() |
| 8 | + class RadioStation |
18 | 9 | {
|
19 |
| - return mFrequency; |
20 |
| - } |
| 10 | + private float mFrequency; |
21 | 11 |
|
22 |
| -} |
23 |
| - |
24 |
| -class StationList : IEnumerable |
25 |
| -{ |
26 |
| - private List<RadioStation> mStations; |
27 |
| - |
28 |
| - public StationList() |
29 |
| - { |
30 |
| - mStations = new List<RadioStation>(); |
31 |
| - } |
| 12 | + public RadioStation(float frequency) |
| 13 | + { |
| 14 | + mFrequency = frequency; |
| 15 | + } |
32 | 16 |
|
33 |
| - public void AddStation(RadioStation station) |
34 |
| - { |
35 |
| - mStations.Add(station); |
36 |
| - } |
| 17 | + public float GetFrequecy() |
| 18 | + { |
| 19 | + return mFrequency; |
| 20 | + } |
37 | 21 |
|
38 |
| - public void RemoveStation(RadioStation station) |
39 |
| - { |
40 |
| - mStations.Remove(station); |
41 | 22 | }
|
42 | 23 |
|
43 |
| - public IEnumerator GetEnumerator() |
| 24 | + class StationList : IEnumerable<RadioStation> |
44 | 25 | {
|
45 |
| - return new StationIterator(mStations); |
46 |
| - } |
47 |
| -} |
| 26 | + List<RadioStation> mStations = new List<RadioStation>(); |
48 | 27 |
|
49 |
| -class StationIterator : IEnumerator |
50 |
| -{ |
51 |
| - private List<RadioStation> mStations; |
52 |
| - private int currentPosition = -1; |
| 28 | + public RadioStation this[int index] |
| 29 | + { |
| 30 | + get { return mStations[index]; } |
| 31 | + set { mStations.Insert(index, value); } |
| 32 | + } |
53 | 33 |
|
54 |
| - public object Current |
55 |
| - { |
56 |
| - get |
| 34 | + public void Add(RadioStation station) |
57 | 35 | {
|
58 |
| - return mStations[currentPosition]; |
| 36 | + mStations.Add(station); |
59 | 37 | }
|
60 |
| - } |
61 | 38 |
|
62 |
| - public StationIterator(List<RadioStation> stations) |
63 |
| - { |
64 |
| - mStations = stations; |
65 |
| - } |
66 |
| - public bool MoveNext() |
67 |
| - { |
68 |
| - if(currentPosition < mStations.Count - 1) |
| 39 | + public void Remove(RadioStation station) |
69 | 40 | {
|
70 |
| - currentPosition = currentPosition + 1; |
71 |
| - return true; |
| 41 | + mStations.Remove(station); |
72 | 42 | }
|
73 | 43 |
|
74 |
| - return false; |
75 |
| - } |
| 44 | + public IEnumerator<RadioStation> GetEnumerator() |
| 45 | + { |
| 46 | + return mStations.GetEnumerator(); |
| 47 | + } |
76 | 48 |
|
77 |
| - public void Reset() |
78 |
| - { |
79 |
| - currentPosition = -1; |
| 49 | + IEnumerator IEnumerable.GetEnumerator() |
| 50 | + { |
| 51 | + //Use can switch to this internal collection if you do not want to transform |
| 52 | + //return this.GetEnumerator(); |
| 53 | + |
| 54 | + //use this if you want to transform the object before rendering |
| 55 | + foreach (var x in mStations) |
| 56 | + { |
| 57 | + yield return x; |
| 58 | + } |
| 59 | + } |
80 | 60 | }
|
81 |
| -} |
82 | 61 |
|
83 |
| -class Program |
84 |
| -{ |
85 |
| - static void Main(string[] args) |
| 62 | + class Program |
86 | 63 | {
|
87 |
| - var stations = new StationList(); |
88 |
| - var station1 = new RadioStation(89); |
89 |
| - stations.AddStation(station1); |
| 64 | + static void Main(string[] args) |
| 65 | + { |
| 66 | + var stations = new StationList(); |
| 67 | + var station1 = new RadioStation(89); |
| 68 | + stations.Add(station1); |
90 | 69 |
|
91 |
| - var station2 = new RadioStation(101); |
92 |
| - stations.AddStation(station2); |
| 70 | + var station2 = new RadioStation(101); |
| 71 | + stations.Add(station2); |
93 | 72 |
|
94 |
| - var station3 = new RadioStation(102); |
95 |
| - stations.AddStation(station3); |
| 73 | + var station3 = new RadioStation(102); |
| 74 | + stations.Add(station3); |
96 | 75 |
|
97 |
| - foreach(RadioStation station in stations) |
98 |
| - { |
99 |
| - Console.WriteLine(station.GetFrequecy()); |
100 |
| - } |
| 76 | + foreach (var x in stations) |
| 77 | + { |
| 78 | + Console.Write(x.GetFrequecy()); |
| 79 | + } |
101 | 80 |
|
102 |
| - stations.RemoveStation(station2); // Will Remove station 101 |
| 81 | + var q = stations.Where(x => x.GetFrequecy() == 89).FirstOrDefault(); |
| 82 | + Console.WriteLine(q.GetFrequecy()); |
103 | 83 |
|
104 |
| - Console.ReadLine(); |
| 84 | + Console.ReadLine(); |
| 85 | + } |
105 | 86 | }
|
106 | 87 | }
|
107 |
| -} |
0 commit comments