Design Pattern

Iterator Pattern

Clean Java-only production-ready implementation.


What Problem It Solves

Provide a way to access elements of an aggregate object sequentially without exposing its underlying representation.

Implementation

// โ”€โ”€โ”€ EXAMPLE 1 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
// WHAT WE ARE IMPLEMENTING:
// A custom HR profiles directory enabling client traversal without exposing
// internal list implementations.
//
// WHERE THE ITERATOR FITS IN:
// ProfileIterator acts as the Iterator interface. ProfileCollection acts as
// the Aggregate collection exposing .createIterator().
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
import java.util.ArrayList;
import java.util.List;

interface Iterator<T> {
    boolean hasNext();
    T next();
}

// --- Custom collection ---
class BrowseHistory {
    private final List<String> urls = new ArrayList<>();

    public void push(String url) { urls.add(url); }

    public Iterator<String> createIterator() {
        return new ListIterator(urls);
    }

    // --- Concrete iterator ---
    private static class ListIterator implements Iterator<String> {
        private final List<String> urls;
        private int index = 0;

        ListIterator(List<String> urls) { this.urls = urls; }

        public boolean hasNext() { return index < urls.size(); }

        public String next() {
            if (!hasNext()) throw new java.util.NoSuchElementException();
            return urls.get(index++);
        }
    }
}

// --- Client ---
public class Main {
    public static void main(String[] args) {
        BrowseHistory history = new BrowseHistory();
        history.push("google.com");
        history.push("github.com");
        history.push("stackoverflow.com");

        Iterator<String> it = history.createIterator();
        while (it.hasNext()) {
            System.out.println("  " + it.next());
        }
    }
}

๐Ÿ’ฌReview

Help Us Improve

How helpful was this walkthrough?

Click a star to rate. We actively use this feedback to refine and update our system design content.

Placeholder
Optional but highly appreciated!

Discussion

Share your thoughts, ask questions, or help others.

Loading comments...