Skip to content

Commit 8e91f1c

Browse files
committed
Update iterator pattern examples
1 parent d03ac39 commit 8e91f1c

File tree

12 files changed

+58
-91
lines changed

12 files changed

+58
-91
lines changed

src/BehavioralPatterns/Iterator/IteratorLibrary/ConceptualExample/Collections/Common/IterableCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ namespace IteratorLibrary.ConceptualExample.Collections.Common;
55
public abstract class IterableCollection : IEnumerable
66
{
77
public abstract IEnumerator GetEnumerator();
8-
}
8+
}

src/BehavioralPatterns/Iterator/IteratorLibrary/ConceptualExample/Collections/WordsCollection.cs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,19 @@ namespace IteratorLibrary.ConceptualExample.Collections;
66

77
public class WordsCollection : IterableCollection
88
{
9-
private readonly List<string> _collection = new List<string>();
10-
private bool _isDirectionReversed = false;
9+
private readonly List<string> _collection = new();
10+
private bool _isDirectionReversed;
1111

12-
public int Count
13-
{
14-
get
15-
{
16-
return _collection.Count;
17-
}
18-
}
12+
public int Count => _collection.Count;
1913

20-
public void Add(string word)
21-
{
22-
_collection.Add(word);
23-
}
14+
public void Add(string word) => _collection.Add(word);
2415

25-
public List<string> GetItems()
26-
{
27-
return _collection;
28-
}
16+
public void ReverseDirection() => _isDirectionReversed = !_isDirectionReversed;
2917

30-
public void ReverseDirection()
31-
{
32-
_isDirectionReversed = !_isDirectionReversed;
33-
}
18+
public override IEnumerator GetEnumerator() => new AlphabeticalOrderIterator(this, _isDirectionReversed);
3419

35-
public override IEnumerator GetEnumerator()
36-
{
37-
return new AlphabeticalOrderIterator(this, _isDirectionReversed);
38-
}
39-
}
20+
public string this[int index] =>
21+
index >= 0 && index < _collection.Count
22+
? _collection[index]
23+
: throw new ArgumentOutOfRangeException(nameof(index));
24+
}

src/BehavioralPatterns/Iterator/IteratorLibrary/ConceptualExample/ConceptualExecutor.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using BuildingBlocks;
1+
using System.Collections;
2+
using BuildingBlocks;
23
using IteratorLibrary.ConceptualExample.Collections;
34

45
namespace IteratorLibrary.ConceptualExample;
@@ -19,18 +20,25 @@ public static void Execute()
1920
};
2021

2122
Console.WriteLine("Straight traversal:");
22-
2323
foreach (var item in collection)
2424
{
2525
Console.WriteLine(item);
2626
}
2727

2828
Console.WriteLine("\nReverse traversal:");
2929
collection.ReverseDirection();
30-
3130
foreach (var item in collection)
3231
{
3332
Console.WriteLine(item);
3433
}
34+
35+
// It is also possible to pass an iterator to a client class instead of giving it access to a whole collection.
36+
// This way, you don't expose the collection to the client at all.
37+
Console.WriteLine("\nReverse traversal using alternative approach:");
38+
IEnumerator enumerator = collection.GetEnumerator();
39+
while (enumerator.MoveNext())
40+
{
41+
Console.WriteLine(enumerator.Current);
42+
}
3543
}
36-
}
44+
}

src/BehavioralPatterns/Iterator/IteratorLibrary/ConceptualExample/Iterators/AlphabeticalOrderIterator.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ namespace IteratorLibrary.ConceptualExample.Iterators;
1010
public class AlphabeticalOrderIterator : Iterator
1111
{
1212
/// <summary>
13-
/// Stores the current traversal position. An iterator may have a lot of
14-
/// other fields for storing iteration state, especially when it is
15-
/// supposed to work with a particular kind of collection.
13+
/// Stores the current traversal position.
14+
/// An iterator may have a lot of other fields for storing iteration state,
15+
/// especially when it is supposed to work with a particular kind of collection.
1616
/// </summary>
1717
private int _position = -1;
1818

1919
private readonly WordsCollection _collection;
20-
private readonly bool _isDirectionReversed = false;
20+
private readonly bool _isDirectionReversed;
2121

2222
public AlphabeticalOrderIterator(WordsCollection collection, bool isDirectionReversed = false)
2323
{
@@ -30,14 +30,11 @@ public AlphabeticalOrderIterator(WordsCollection collection, bool isDirectionRev
3030
}
3131
}
3232

33-
public override object Current()
34-
{
35-
return _collection.GetItems()[_position];
36-
}
33+
public override object Current() => _collection[_position];
3734

3835
public override bool MoveNext()
3936
{
40-
int updatedPosition = _position + (_isDirectionReversed ? -1 : 1);
37+
var updatedPosition = _position + (_isDirectionReversed ? -1 : 1);
4138

4239
if (updatedPosition >= 0 && updatedPosition < _collection.Count)
4340
{
@@ -48,8 +45,5 @@ public override bool MoveNext()
4845
return false;
4946
}
5047

51-
public override void Reset()
52-
{
53-
_position = _isDirectionReversed ? _collection.Count : -1;
54-
}
55-
}
48+
public override void Reset() => _position = _isDirectionReversed ? _collection.Count : -1;
49+
}

src/BehavioralPatterns/Iterator/IteratorLibrary/ConceptualExample/Iterators/Common/Iterator.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ public abstract class Iterator : IEnumerator
77
object IEnumerator.Current => Current();
88

99
public abstract object Current();
10-
1110
public abstract bool MoveNext();
12-
1311
public abstract void Reset();
14-
}
12+
}

src/BehavioralPatterns/Iterator/IteratorLibrary/Executor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ public override void Execute()
1313
ConceptualExecutor.Execute();
1414
MusicFestivalsExecutor.Execute();
1515
}
16-
}
16+
}

src/BehavioralPatterns/Iterator/IteratorLibrary/MusicFestivalsExample/Collections/Common/IIterableCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ namespace IteratorLibrary.MusicFestivalsExample.Collections.Common;
55
public interface IIterableCollection
66
{
77
IIterator CreateIterator();
8-
}
8+
}

src/BehavioralPatterns/Iterator/IteratorLibrary/MusicFestivalsExample/Collections/FestivalCollection.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,13 @@ namespace IteratorLibrary.MusicFestivalsExample.Collections;
66

77
public class FestivalCollection : IIterableCollection
88
{
9-
private readonly List<MusicFestival> _festivals = new List<MusicFestival>();
9+
private readonly List<MusicFestival> _festivals = new();
1010

11-
public int Count
12-
{
13-
get { return _festivals.Count; }
14-
}
11+
public int Count => _festivals.Count;
1512

16-
public void Add(MusicFestival festival)
17-
{
18-
_festivals.Add(festival);
19-
}
13+
public void Add(MusicFestival festival) => _festivals.Add(festival);
2014

21-
public MusicFestival Get(int index)
22-
{
23-
return _festivals[index];
24-
}
15+
public MusicFestival Get(int index) => _festivals[index];
2516

26-
public IIterator CreateIterator()
27-
{
28-
return new FestivalIterator(this);
29-
}
30-
}
17+
public IIterator CreateIterator() => new FestivalIterator(this);
18+
}

src/BehavioralPatterns/Iterator/IteratorLibrary/MusicFestivalsExample/Iterators/Common/IIterator.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ public interface IIterator
55
int Step { get; set; }
66

77
MusicFestival First();
8-
98
MusicFestival Current();
10-
119
bool MoveNext();
12-
1310
void Reset();
14-
}
11+
}

src/BehavioralPatterns/Iterator/IteratorLibrary/MusicFestivalsExample/Iterators/FestivalIterator.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ public FestivalIterator(FestivalCollection collection)
1616
_step = 1;
1717
}
1818

19+
// It's probably more logical to have a method for changing the step.
20+
// However, for demo purposes the idea was to show that iterator interface can contain properties too.
1921
public int Step
2022
{
21-
get
22-
{
23-
return _step;
24-
}
23+
get => _step;
2524
set
2625
{
26+
if (value <= 0)
27+
{
28+
throw new ArgumentOutOfRangeException(nameof(value), "The step can be only positive value. The current iterator doesn't support going backward.");
29+
}
30+
2731
_step = value;
2832
}
2933
}
@@ -34,14 +38,11 @@ public MusicFestival First()
3438
return Current();
3539
}
3640

37-
public MusicFestival Current()
38-
{
39-
return _collection.Get(_position);
40-
}
41+
public MusicFestival Current() => _collection.Get(_position);
4142

4243
public bool MoveNext()
4344
{
44-
int updatedPosition = _position + _step;
45+
var updatedPosition = _position + _step;
4546

4647
if (updatedPosition < _collection.Count)
4748
{
@@ -52,8 +53,5 @@ public bool MoveNext()
5253
return false;
5354
}
5455

55-
public void Reset()
56-
{
57-
_position = -1;
58-
}
59-
}
56+
public void Reset() => _position = -1;
57+
}

0 commit comments

Comments
 (0)