codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎭

Design Patterns

23 / 100 topics
19Introduction to Behavioral Patterns20Chain of Responsibility Pattern21Command Pattern22Interpreter Pattern23Iterator Pattern24Mediator Pattern25Memento Pattern26Observer Pattern27State Pattern28Strategy Pattern29Template Method Pattern30Visitor Pattern33Practical Exercises for Behavioral Patterns
Tutorials/Design Patterns/Iterator Pattern
🎭Design Patterns

Iterator Pattern

Updated 2026-05-15
10 min read

Iterator Pattern

Introduction

In software design, patterns are reusable solutions to common problems. The Iterator Pattern is a behavioral design pattern that provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. This pattern is particularly useful when you want to traverse a collection of objects without knowing the internal structure of the collection.

Concept

The Iterator Pattern consists of several key components:

  1. Iterator: Defines an interface for accessing and traversing elements.
  2. ConcreteIterator: Implements the Iterator interface. It keeps track of the current position in the traversal of the aggregate.
  3. Aggregate: Defines an interface for creating an iterator object.
  4. ConcreteAggregate: Implements the Aggregate interface. It returns an instance of the ConcreteIterator.

The main advantage of using the Iterator Pattern is that it encapsulates the internal structure of the collection, allowing clients to traverse the elements without needing to know how they are stored or organized.

Examples

Let's explore a practical example of the Iterator Pattern in action. We'll create a simple collection of books and iterate over them using an iterator.

Step 1: Define the Aggregate Interface

First, we define the Aggregate interface that will be implemented by our concrete aggregate class.

JavaScript
1interface Aggregate {
2createIterator(): Iterator;
3}

Step 2: Implement the ConcreteAggregate Class

Next, we implement the ConcreteAggregate class that holds a collection of books and provides an iterator to traverse them.

JavaScript
1class BookCollection implements Aggregate {
2private books: string[];
3
4constructor() {
5 this.books = [];
6}
7
8addBook(book: string): void {
9 this.books.push(book);
10}
11
12createIterator(): Iterator {
13 return new BookIterator(this);
14}
15}

Step 3: Define the Iterator Interface

We define the Iterator interface that specifies methods for traversing elements.

JavaScript
1interface Iterator {
2hasNext(): boolean;
3next(): string | null;
4}

Step 4: Implement the ConcreteIterator Class

Finally, we implement the ConcreteIterator class that keeps track of the current position in the collection and provides methods to traverse it.

JavaScript
1class BookIterator implements Iterator {
2private bookCollection: BookCollection;
3private currentIndex: number;
4
5constructor(bookCollection: BookCollection) {
6 this.bookCollection = bookCollection;
7 this.currentIndex = 0;
8}
9
10hasNext(): boolean {
11 return this.currentIndex < this.bookCollection.books.length;
12}
13
14next(): string | null {
15 if (this.hasNext()) {
16 const book = this.bookCollection.books[this.currentIndex];
17 this.currentIndex++;
18 return book;
19 }
20 return null;
21}
22}

Step 5: Use the Iterator Pattern

Now, let's use our BookCollection and BookIterator to add some books and iterate over them.

JavaScript
1const collection = new BookCollection();
2collection.addBook('Design Patterns');
3collection.addBook('Clean Code');
4collection.addBook('Refactoring');
5
6const iterator = collection.createIterator();
7
8while (iterator.hasNext()) {
9console.log(iterator.next());
10}

Output

Output
Design Patterns
Clean Code
Refactoring

In this example, we have successfully implemented the Iterator Pattern. The BookCollection class acts as the aggregate, and the BookIterator class provides a way to traverse the books without exposing the internal array structure.

What's Next?

Now that you've learned about the Iterator Pattern, you might want to explore other behavioral patterns such as the Mediator Pattern. The Mediator Pattern is useful for managing complex interactions between objects by centralizing communication through a mediator object. Stay tuned for more tutorials on design patterns!


PreviousInterpreter PatternNext Mediator Pattern

Recommended Gear

Interpreter PatternMediator Pattern