Design patterns: Iterator

date: · 3 min
author: Mauto
series: Design Patterns/ part 17/24

The classic Gang of Four patterns, one per part, with practical PHP examples.

  1. Design Patterns
  2. Design patterns: Singleton
  3. Design patterns: Factory Method
  4. Design patterns: Abstract Factory
  5. Design patterns: Builder
  6. Design patterns: Prototype
  7. Design patterns: Adapter
  8. Bridge Pattern in PHP: What It Is, When to Use It, with Examples
  9. Design patterns: Composite
  10. Design patterns: Decorator
  11. Design patterns: Facade, What It Is and How to Use It (PHP)
  12. Design patterns: Flyweight
  13. Design patterns: Proxy
  14. Design patterns: Chain of Responsibility Explained (PHP)
  15. Design patterns: Command
  16. Design patterns: Interpreter
  17. Design patterns: Iterator
  18. Design patterns: Mediator
  19. Design patterns: Memento
  20. Design patterns: Observer
  21. Design patterns: State, What It Is and How to Use It (PHP)
  22. Design patterns: Strategy
  23. Design patterns: Template Method
  24. Design patterns: Visitor
the whole guide >

The Iterator Pattern is a behavioral design pattern that provides a way to access the elements of a collection sequentially without exposing its internal representation. This pattern separates the iteration logic from the collection, allowing you to traverse a data structure transparently and flexibly, regardless of its underlying implementation.

Strengths

  1. Separation of Concerns: The Iterator Pattern separates the code that manages the data structure from the code that traverses the collection. This simplifies maintenance and improves code readability.
  2. Uniform Access: It provides a uniform interface for traversing different data structures, making client code independent of the collection’s internal representation.
  3. Easy to Swap the Data Structure: The pattern makes it easy to change the underlying data structure (array, list, set, etc.) without having to modify the client code that uses the iterator.
  4. Flexibility and Reuse: Adding or changing an iterator’s behavior (for example, for filtering or reverse iteration) is simple and requires no changes to the original collection.
  5. Compatibility with the Single Responsibility Principle: With the Iterator Pattern, the iteration logic is delegated to a dedicated iterator, letting the collection focus on managing data and improving the code’s design.

Use Case: A Book Collection

Imagine we have a collection of books and want to be able to iterate over it without exposing the internal details of its implementation.

PHP Code

// Defines the interface for the iterator.
// Not called "Iterator": PHP already has a built-in interface with that name.
interface IteratorInterface {
    public function hasNext(): bool;
    public function next();
}

// Book collection
class BookCollection {
    private $books = [];

    public function addBook($book) {
        $this->books[] = $book;
    }

    public function createIterator(): IteratorInterface {
        return new BookIterator($this->books);
    }
}

// Iterator implementation for the book collection
class BookIterator implements IteratorInterface {
    private $books;
    private $position = 0;

    public function __construct($books) {
        $this->books = $books;
    }

    public function hasNext(): bool {
        return $this->position < count($this->books);
    }

    public function next() {
        return $this->books[$this->position++];
    }
}

// Use case
$collection = new BookCollection();
$collection->addBook("The Catcher in the Rye");
$collection->addBook("To Kill a Mockingbird");
$collection->addBook("1984");

// Using the iterator to traverse the collection
$iterator = $collection->createIterator();
while ($iterator->hasNext()) {
    echo $iterator->next() . PHP_EOL;
}

Explanation

  • IteratorInterface interface: Defines the standard methods an iterator must implement (hasNext() and next()).
  • BookCollection class: Holds the book collection and a createIterator method that returns an iterator.
  • BookIterator class: Implements the IteratorInterface interface and provides the mechanism for traversing the book collection.
  • Usage: We create a book collection, add a few books, and use the iterator to iterate over the collection’s elements without directly accessing its internal structure.

Conclusion

The Iterator Pattern offers a flexible way to traverse complex collections, improving code maintainability and separation of concerns. It is ideal when the data structure may change or when you want to hide the traversal logic from client code.

BACK TO THE GUIDE

Copied