Skip to main content

🧩 Iterator Pattern

βœ… Intent​

  • Provide a way to access elements of a collection sequentially without exposing its underlying structure
  • Useful when a God Object contains a complex data structure with hardcoded loop logic

βœ… Motivation​

  • To offer a consistent way to traverse internal data structures (lists, trees, etc.)
  • Allows elements to be handled safely and concisely from the outside

βœ… When to Use​

  • When looping over custom data structures is required
  • When for or while loops are scattered and duplicated, and traversal logic lacks consistency

βœ… Code Example​

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

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

class UserCollection implements IterableCollection<string> {
private users: string[] = [];

add(user: string) {
this.users.push(user);
}

createIterator(): Iterator<string> {
let index = 0;
const users = this.users;

return {
hasNext() {
return index < users.length;
},
next() {
return users[index++];
},
} as Iterator<string>;
}
}

// εˆ©η”¨δΎ‹
const users = new UserCollection();
users.add("Hiroshi");
users.add("Satoshi");

const it = users.createIterator();
while (it.hasNext()) {
console.log(it.next());
}

βœ… Explanation​

This code applies the Iterator pattern to provide a way to access elements within a collection (UserCollection) in sequence.
The Iterator pattern allows elements of a collection to be accessed one at a time, while hiding the internal structure of the collection.

1. Overview of the Iterator Pattern​

  • Iterator: Defines an interface for accessing elements sequentially

    • In this code, represented by Iterator<T>
  • IterableCollection: Defines an interface for creating iterators

    • In this code, represented by IterableCollection<T>
  • ConcreteCollection: Implements the collection and provides a concrete iterator

    • In this code, represented by UserCollection

2. Key Classes and Their Roles​

  • Iterator<T>

    • The interface for iterators
    • hasNext checks for the existence of the next element
    • next retrieves the next element
  • IterableCollection<T>

    • Interface for collections that can produce iterators
    • Declares the createIterator method
  • UserCollection

    • A concrete collection class
    • Maintains an internal list of usernames (string)
    • Provides an add method to insert elements
    • Implements the createIterator method to return an iterator

3. UML Class Diagram​

4. Benefits of the Iterator Pattern​

  • Encapsulation of Internal Structure: Allows access to elements without revealing the internal organization of the collection
  • Consistent Operations: Enables uniform access to different types of collections through a common interface
  • Extensibility: New collections can be added by implementing Iterator and IterableCollection

This design enables safe and consistent traversal of collection elements without needing to understand the internal structureβ€”especially useful when dealing with complex data models.