Behavioral Complexity: Medium

Iterator Pattern

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

The Problem

A product catalog can be browsed in many ways — sorted by price, filtered by category, or paginated. Embedding each traversal in the collection class makes it bloated and hard to maintain. Adding a new traversal strategy requires modifying the collection.

The Solution

The Iterator pattern extracts each traversal strategy into its own iterator object. The product collection provides factory methods to create the appropriate iterator, and each iterator encapsulates its own traversal logic.

Structure

  • Iterator (Iterator<T> interface) — Declares hasNext(), next(), reset().
  • Concrete IteratorsPriceAscendingIterator, CategoryIterator, PaginatedIterator.
  • Aggregate / CollectionProductCollection with factory methods for creating iterators.

Implementation

The collection holds 12 products across three categories (electronics, sports, books) with three traversal strategies:

  1. PriceAscendingIterator — Products sorted by price low to high.
  2. CategoryIterator — Products filtered by a specific category.
  3. PaginatedIterator — Products grouped into pages of configurable size.
export interface Iterator<T> {
  hasNext(): boolean;
  next(): T;
  reset(): void;
}

export interface IterableCollection<T> {
  createIterator(): Iterator<T>;
}

export interface Product {
  id: string;
  name: string;
  price: number;
  category: string;
}

NestJS Integration

The ProductCollection is created in the controller and populated with sample data. The iterators are plain classes, not NestJS providers, since they hold traversal state specific to each request. The controller selects the appropriate iterator based on query parameters.

When to Use

  • You need to traverse a collection in multiple ways without modifying the collection class.
  • You want to hide the collection’s internal structure from clients.
  • You need uniform iteration over different types of collections.

When NOT to Use

  • The collection only ever needs one traversal strategy — a simple for loop or array method is clearer.
  • The collection is a simple array/list where built-in iterators are sufficient.