Skip to content

Commit 41bff47

Browse files
committed
iterator pattern changes
1 parent d3c6a3e commit 41bff47

File tree

1 file changed

+54
-74
lines changed

1 file changed

+54
-74
lines changed

IteratorPattern/Program.cs

Lines changed: 54 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,103 +5,83 @@
55

66
namespace IteratorPattern
77
{
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
189
{
19-
return mFrequency;
20-
}
10+
private float mFrequency;
2111

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+
}
3216

33-
public void AddStation(RadioStation station)
34-
{
35-
mStations.Add(station);
36-
}
17+
public float GetFrequecy()
18+
{
19+
return mFrequency;
20+
}
3721

38-
public void RemoveStation(RadioStation station)
39-
{
40-
mStations.Remove(station);
4122
}
4223

43-
public IEnumerator GetEnumerator()
24+
class StationList : IEnumerable<RadioStation>
4425
{
45-
return new StationIterator(mStations);
46-
}
47-
}
26+
List<RadioStation> mStations = new List<RadioStation>();
4827

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+
}
5333

54-
public object Current
55-
{
56-
get
34+
public void Add(RadioStation station)
5735
{
58-
return mStations[currentPosition];
36+
mStations.Add(station);
5937
}
60-
}
6138

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)
6940
{
70-
currentPosition = currentPosition + 1;
71-
return true;
41+
mStations.Remove(station);
7242
}
7343

74-
return false;
75-
}
44+
public IEnumerator<RadioStation> GetEnumerator()
45+
{
46+
return mStations.GetEnumerator();
47+
}
7648

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+
}
8060
}
81-
}
8261

83-
class Program
84-
{
85-
static void Main(string[] args)
62+
class Program
8663
{
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);
9069

91-
var station2 = new RadioStation(101);
92-
stations.AddStation(station2);
70+
var station2 = new RadioStation(101);
71+
stations.Add(station2);
9372

94-
var station3 = new RadioStation(102);
95-
stations.AddStation(station3);
73+
var station3 = new RadioStation(102);
74+
stations.Add(station3);
9675

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+
}
10180

102-
stations.RemoveStation(station2); // Will Remove station 101
81+
var q = stations.Where(x => x.GetFrequecy() == 89).FirstOrDefault();
82+
Console.WriteLine(q.GetFrequecy());
10383

104-
Console.ReadLine();
84+
Console.ReadLine();
85+
}
10586
}
10687
}
107-
}

0 commit comments

Comments
 (0)