Design Patterns Mastery

Iterator Pattern: Unified traversal of collections

1 Views Updated 5/4/2026

The Iterator Pattern

The Iterator Pattern provides a way to access the elements of an aggregate object (like a List or Tree) sequentially without exposing its underlying internal structure (like nodes, pointers, or indices).

1. Real-World .NET Example: IEnumerable

In C#, almost every collection implements IEnumerable. This is the Iterator pattern in its purest form. You don't need to know if the data is in an Array, a LinkedList, or a Database; you just call foreach.

public interface IIterator 
{
    bool HasNext();
    object Next();
}

2. Encapsulation

The biggest benefit is **Encapsulation**. If you change your internal storage from a Dictionary to a List, you only update the Iterator. All your UI code and business logic that uses foreach remains 100% unchanged.

4. Interview Mastery

Q: "Why does C# use an Enumerator state machine instead of a simple integer index for iterating?"

Architect Answer: "Because not all collections are indexable. A binary tree or a linked list doesn't have an 'Index 5'. By using an Enumerator object that tracks its own internal state, .NET can provide a unified way to loop over any data shape. It also allows for **Deferred Execution** (Linq), where the data is only calculated the moment the iterator moves forward, saving massive amounts of memory."

Design Patterns Mastery
1. Introduction to Design Patterns
Introduction to GoF Patterns: Why patterns matter? SOLID Principles: The foundation of all patterns DRY, KISS, and YAGNI (Architectural Philosophy)
2. Creational Patterns
Singleton Pattern: Thread-safety & Captive Dependencies Factory Method: Abstracting complex object creation Abstract Factory: Creating families of related objects Builder Pattern: Constructing complex fluents Prototype Pattern: Cloning high-cost objects
3. Structural Patterns
Adapter Pattern: Bridging incompatible interfaces Bridge Pattern: Decoupling abstraction from implementation Composite Pattern: Managing tree structures & hierarchies Decorator Pattern: Enhancing behavior without inheritance Facade Pattern: Simplifying complex library subsystems Flyweight Pattern: Drastically reducing memory footprint Proxy Pattern: Interception, Lazy-Loading, and Security
4. Behavioral Patterns
Chain of Responsibility: Middleware & Pipeline Architecture Command Pattern: Implementing Undo/Redo & Queueing Interpreter Pattern: Building domain-specific languages Iterator Pattern: Unified traversal of collections Mediator Pattern: Decoupling components with MediatR Memento Pattern: Capturing and restoring object state Observer Pattern: Pub/Sub & Event-driven architecture State Pattern: Managing complex object lifecycles Strategy Pattern: Swappable algorithms at runtime Template Method: Defining skeleton algorithms Visitor Pattern: Separating operations from data structures
5. Modern Enterprise & Cloud Patterns
Repository & Unit of Work (The EF Core Standard) CQRS Pattern (Command Query Responsibility Segregation) Circuit Breaker & Retry Patterns (Resilience with Polly) Dependency Injection Pattern (The Modern King)