Knowledge Guide
HomeOO & Low-Level DesignBehavioral

Iterator Pattern

Iterator Pattern provides a way to access elements of a collection object sequentially without exposing its underlying structure. This pattern introduces the concept of iterator. iterator is a tool through which you can traverse a collection (arrays, lists, or even more complex data structures) in a standardized manner.

Consider a scenario when you are working with a complex data structure, such as a graph or a tree. Extracting the data from such data structure is tricky, especially if don't want to show the internal structure of the data structure to the client. Without the use of an iterator pattern, the code to navigate through the elements will be complex and tightly coupled.

Suppose you have a library of books and want to display the titles of all the books. If you don't have an iterator pattern, you will be required to know how books are stored in the library. This will lead to an inefficient code that will be difficult to maintain.

This is elegantly solved by the Iterator pattern, which separates the traversal mechanism from the data structure itself. It involves two key players: the 'Iterable' that provides the iterator and the 'Iterator' that encapsulates the traversal logic.

Iterator Pattern
Iterator Pattern

The image above shows how we create an iterator for the library using the Iterator pattern so that external code can iterate through the books without having to know how they are internally stored. To traverse the collection, the iterator provides functions like hasNext() and next().

Read-World Example

Iterator Pattern - Music Player Application
Iterator Pattern - Music Player Application

In the image, you can see a music player application on smartphone. There are 'next' and 'previous' buttons on the phone's screen in addition to a list of songs. This arrangement represents how the Iterator pattern is used to move through the playlist.

Like an iterator in programming lets you iterate through items in a collection, users can easily navigate through their music playlist in this real-world scenario, mirroring the Iterator pattern. The application's "next" and "previous" buttons function as the next() and previous() methods of an iterator's real-world equivalents, offering a straightforward and user-friendly method of accessing songs in the desired order. This illustration shows how the idea of iterators is present in both software development and regular user experiences.

Structure of Iterator Pattern

Iterator Pattern - Class Diagram
Iterator Pattern - Class Diagram

Implementation of Iterator Pattern

Let's consider a book collection in a library. We want to provide a way to iterate through the books without exposing the underlying data structure of the collection.

// Iterator Interface interface Iterator { hasNext(): Boolean next(): Object } // Concrete Iterator for Book Collection class BookIterator implements Iterator { private currentIndex = 0 private collection: BookCollection BookIterator(BookCollection collection) { this.collection = collection } hasNext(): Boolean { return currentIndex < collection.getLength() } next(): Object { if (this.hasNext()) { book = collection.getAt(currentIndex) currentIndex += 1 return book } return null } } // Aggregate Interface interface Aggregate { createIterator(): Iterator } // Concrete Aggregate for Books class BookCollection implements Aggregate { private books: Array BookCollection() { books = new Array() } addBook(book: Book) { books.append(book) } getLength(): Integer { return books.length } getAt(index: Integer): Book { return books[index] } createIterator(): Iterator { return new BookIterator(this) } } // Client Code main() { bookCollection = new BookCollection() bookCollection.addBook(new Book("Book 1")) bookCollection.addBook(new Book("Book 2")) // Add more books... iterator = bookCollection.createIterator() while (iterator.hasNext()) { book = iterator.next() print(book.title) } }

Let's look at the implementation of this example in multiple programming languages.

Implementation

java
import java.util.ArrayList;
import java.util.List;

class Book {
    String title;

    Book(String title) {
        this.title = title;
    }
}

interface Iterator {
    boolean hasNext();
    Book next();
}

class BookCollection {
    private List<Book> books = new ArrayList<>();

    void addBook(Book book) {
        books.add(book);
    }

    Iterator createIterator() {
        return new BookIterator(this);
    }

    Book get(int index) {
        return books.get(index);
    }

    int size() {
        return books.size();
    }

    private class BookIterator implements Iterator {
        private BookCollection collection;
        private int currentIndex = 0;

        BookIterator(BookCollection collection) {
            this.collection = collection;
        }

        public boolean hasNext() {
            return currentIndex < collection.size();
        }

        public Book next() {
            return hasNext() ? collection.get(currentIndex++) : null;
        }
    }
}

public class Solution {
    public static void main(String[] args) {
        BookCollection collection = new BookCollection();
        collection.addBook(new Book("Book 1"));
        collection.addBook(new Book("Book 2"));

        Iterator iterator = collection.createIterator();
        while (iterator.hasNext()) {
            Book book = iterator.next();
            System.out.println(book.title);
        }
    }
}

Application of Iterator Pattern

Pros and Cons

ProsCons
Separation of Concerns: Separates the collection's internal structure and the algorithm to traverse it.Complexity: Can add extra complexity and overhead to simple collections.
Flexibility: Provides multiple ways to traverse a collection (forward, backward, filtered).Iterator Invalidation: Iterators can become invalid if the collection is modified during iteration.
Abstraction and Consistency: Provides a uniform interface for multiple types of collections.Concurrent Modification: Standard iterators typically don't support safe concurrent modification and iteration.
Control over Iteration: Gives more control over the pace and order of traversal.Statefulness: Iterators maintain state, which needs careful handling to avoid errors.
Memory Efficiency: Efficient in memory usage, especially for large collections.Simplicity vs. Overhead: For simple tasks, using an iterator might be more complex than necessary.

The Iterator pattern is a fundamental design pattern that is especially useful in scenarios that require a standardized way to access collection elements. Though it adds more levels of abstraction, making the codebase more structured and manageable, it's vital to take into account the possible overheads and complications it may cause.

🤖 Don't fully get this? Learn it with Claude

Stuck on Iterator Pattern? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Iterator Pattern** (OO & Low-Level Design) and want to truly understand it. Explain Iterator Pattern from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Iterator Pattern** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Iterator Pattern** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Iterator Pattern** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes