🎨 Design Patterns (Gang of Four)
Beginner
What is the Iterator design pattern?
Answer
The Iterator pattern provides a standard way to sequentially access elements of a collection without exposing its underlying representation (array, tree, linked list, etc.). It defines a hasNext() and next() interface. This allows the same client code to traverse different collection types uniformly. Java's Iterator interface and Python's __iter__/__next__ protocol are the most prominent implementations. The pattern also allows multiple independent iterators on the same collection simultaneously, each maintaining its own traversal state without interfering with each other.