Skip to content

Commit 6c73898

Browse files
committed
Iterator patten example
1 parent 99cdad8 commit 6c73898

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibilityPatter
3131
EndProject
3232
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommandPattern", "CommandPattern\CommandPattern.csproj", "{25E92DA1-1205-4D53-85F4-B77BC92DCF5A}"
3333
EndProject
34+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IteratorPattern", "IteratorPattern\IteratorPattern.csproj", "{BB1AE146-315A-44F8-A90B-77E386C65678}"
35+
EndProject
3436
Global
3537
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3638
Debug|Any CPU = Debug|Any CPU
@@ -93,6 +95,10 @@ Global
9395
{25E92DA1-1205-4D53-85F4-B77BC92DCF5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
9496
{25E92DA1-1205-4D53-85F4-B77BC92DCF5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
9597
{25E92DA1-1205-4D53-85F4-B77BC92DCF5A}.Release|Any CPU.Build.0 = Release|Any CPU
98+
{BB1AE146-315A-44F8-A90B-77E386C65678}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
99+
{BB1AE146-315A-44F8-A90B-77E386C65678}.Debug|Any CPU.Build.0 = Debug|Any CPU
100+
{BB1AE146-315A-44F8-A90B-77E386C65678}.Release|Any CPU.ActiveCfg = Release|Any CPU
101+
{BB1AE146-315A-44F8-A90B-77E386C65678}.Release|Any CPU.Build.0 = Release|Any CPU
96102
EndGlobalSection
97103
GlobalSection(SolutionProperties) = preSolution
98104
HideSolutionNode = FALSE
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

IteratorPattern/Program.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace IteratorPattern
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()
18+
{
19+
return mFrequency;
20+
}
21+
22+
}
23+
24+
class StationList : IEnumerable
25+
{
26+
private List<RadioStation> mStations;
27+
28+
public StationList()
29+
{
30+
mStations = new List<RadioStation>();
31+
}
32+
33+
public void AddStation(RadioStation station)
34+
{
35+
mStations.Add(station);
36+
}
37+
38+
public void RemoveStation(RadioStation station)
39+
{
40+
mStations.Remove(station);
41+
}
42+
43+
public IEnumerator GetEnumerator()
44+
{
45+
return new StationIterator(mStations);
46+
}
47+
}
48+
49+
class StationIterator : IEnumerator
50+
{
51+
private List<RadioStation> mStations;
52+
private int currentPosition = -1;
53+
54+
public object Current
55+
{
56+
get
57+
{
58+
return mStations[currentPosition];
59+
}
60+
}
61+
62+
public StationIterator(List<RadioStation> stations)
63+
{
64+
mStations = stations;
65+
}
66+
public bool MoveNext()
67+
{
68+
if(currentPosition < mStations.Count - 1)
69+
{
70+
currentPosition = currentPosition + 1;
71+
return true;
72+
}
73+
74+
return false;
75+
}
76+
77+
public void Reset()
78+
{
79+
currentPosition = -1;
80+
}
81+
}
82+
83+
class Program
84+
{
85+
static void Main(string[] args)
86+
{
87+
var stations = new StationList();
88+
var station1 = new RadioStation(89);
89+
stations.AddStation(station1);
90+
91+
var station2 = new RadioStation(101);
92+
stations.AddStation(station2);
93+
94+
var station3 = new RadioStation(102);
95+
stations.AddStation(station3);
96+
97+
foreach(RadioStation station in stations)
98+
{
99+
Console.WriteLine(station.GetFrequecy());
100+
}
101+
102+
stations.RemoveStation(station2); // Will Remove station 101
103+
104+
Console.ReadLine();
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)