Skip to content

Commit 0aecf49

Browse files
committed
Add Iterator Design Pattern implementation
Implemented the Iterator pattern with `Book`, `BookCollection`, `BookIterator`, `IBookCollection`, and `IIterator` classes and interfaces. Updated `Program.cs` to demonstrate the pattern with a collection of books and an iterator. Updated `README.md` to document the Iterator pattern, its purpose, usage example, and test locations. Added `IteratorTests` to verify correct iteration over a `BookCollection`. Ensured adherence to the Iterator pattern principles by providing sequential access to collection elements without exposing the underlying representation.
1 parent d06ec7b commit 0aecf49

File tree

8 files changed

+111
-1
lines changed

8 files changed

+111
-1
lines changed

DesignPatterns/Iterator/Book.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace DesignPatterns.Iterator;
2+
3+
public sealed class Book
4+
{
5+
public string Title { get; }
6+
public string Author { get; }
7+
8+
public Book(string title, string author)
9+
{
10+
Title = title ?? string.Empty;
11+
Author = author ?? string.Empty;
12+
}
13+
14+
public override string ToString() => $"{Title} by {Author}";
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace DesignPatterns.Iterator;
2+
3+
public class BookCollection : IBookCollection
4+
{
5+
private readonly List<Book> _books = new();
6+
7+
public void Add(Book book) => _books.Add(book);
8+
9+
public IIterator CreateIterator() => new BookIterator(_books);
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace DesignPatterns.Iterator;
2+
3+
public class BookIterator : IIterator
4+
{
5+
private readonly List<Book> _books;
6+
private int _position = 0;
7+
8+
public BookIterator(List<Book> books)
9+
{
10+
_books = books ?? new List<Book>();
11+
}
12+
13+
public bool HasNext() => _position < _books.Count;
14+
15+
public Book Next()
16+
{
17+
if (!HasNext()) throw new InvalidOperationException();
18+
return _books[_position++];
19+
}
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace DesignPatterns.Iterator;
2+
3+
public interface IBookCollection
4+
{
5+
IIterator CreateIterator();
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace DesignPatterns.Iterator;
2+
3+
public interface IIterator
4+
{
5+
bool HasNext();
6+
Book Next();
7+
}

DesignPatterns/Program.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using DesignPatterns.Flyweight;
1212
using DesignPatterns.Proxy;
1313
using DesignPatterns.Chain;
14+
using DesignPatterns.Iterator;
1415

1516
Console.WriteLine("Factory pattern demo:");
1617

@@ -98,6 +99,18 @@
9899
nested.Add(new CircleGraphic());
99100
Console.WriteLine(nested.Draw());
100101

102+
// Iterator demo
103+
Console.WriteLine();
104+
Console.WriteLine("Iterator pattern demo:");
105+
var books = new BookCollection();
106+
books.Add(new Book("Title1", "Author1"));
107+
books.Add(new Book("Title2", "Author2"));
108+
109+
var it = books.CreateIterator();
110+
while (it.HasNext())
111+
{
112+
Console.WriteLine(it.Next());
113+
}
101114

102115
// Chain of Responsibility demo
103116
Console.WriteLine();

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Design Patterns Examples
33
This solution demonstrates simple design pattern examples in C# targeting .NET 10 (C# 14).
44

55
Projects
6-
- `DesignPatterns` - Console demo showing Factory, Singleton, Builder, Prototype, Abstract Factory, Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy, Chain of Responsibility, and Command patterns.
6+
- `DesignPatterns` - Console demo showing Factory, Singleton, Builder, Prototype, Abstract Factory, Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy, Chain of Responsibility, Command, and Iterator patterns.
77
- Factory implementation: `DesignPatterns/Factories`
88
- Singleton implementation: `DesignPatterns/Singleton/Logger.cs`
99
- Builder implementation: `DesignPatterns/Builder/HouseBuilder.cs` and `DesignPatterns/Builder/House.cs`
@@ -18,6 +18,7 @@ Projects
1818
- Proxy implementation: `DesignPatterns/Proxy/*`
1919
- Chain of Responsibility implementation: `DesignPatterns/Chain/*`
2020
- Command implementation: `DesignPatterns/Command/*`
21+
- Iterator implementation: `DesignPatterns/Iterator/*`
2122
- `Tests` - xUnit tests covering the examples.
2223

2324
Included patterns and brief docs
@@ -188,6 +189,19 @@ Included patterns and brief docs
188189
remote.PressUndo();
189190
```
190191
192+
- Iterator Pattern
193+
- Purpose: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
194+
- Example: `DesignPatterns/Iterator/*` contains `Book`, `BookCollection`, `BookIterator`, and interfaces `IBookCollection`, `IIterator`.
195+
- Usage snippet:
196+
197+
```csharp
198+
var books = new BookCollection();
199+
books.Add(new Book("Title1", "Author1"));
200+
books.Add(new Book("Title2", "Author2"));
201+
var it = books.CreateIterator();
202+
while (it.HasNext()) Console.WriteLine(it.Next());
203+
```
204+
191205
Tests
192206
- Tests are written with xUnit in the `Tests` project.
193207
- Factory tests: `Tests/ShapeFactoryTests.cs`
@@ -204,6 +218,7 @@ Tests
204218
- Proxy tests: `Tests/ProxyTests.cs`
205219
- Chain tests: `Tests/ChainTests.cs`
206220
- Command tests: `Tests/CommandTests.cs`
221+
- Iterator tests: `Tests/IteratorTests.cs`
207222
208223
Common commands
209224
- Build solution: `dotnet build`

Tests/IteratorTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using DesignPatterns.Iterator;
2+
3+
namespace Tests;
4+
5+
public class IteratorTests
6+
{
7+
[Fact]
8+
public void BookIterator_IteratesOverCollection()
9+
{
10+
var collection = new BookCollection();
11+
collection.Add(new Book("Title1", "Author1"));
12+
collection.Add(new Book("Title2", "Author2"));
13+
14+
var it = collection.CreateIterator();
15+
var titles = new List<string>();
16+
17+
while (it.HasNext())
18+
{
19+
titles.Add(it.Next().Title);
20+
}
21+
22+
Assert.Equal(new[] { "Title1", "Title2" }, titles);
23+
}
24+
}

0 commit comments

Comments
 (0)