Iterator Design Pattern

Iterator design pattern is one of the most popular and relatively simple design pattern which is commonly used in Java programming language. Iterator pattern provides a standard way to traverse a group of objects sequentially without exposing its internal implementation. The Iterator pattern takes the responsibility of traversal out of the collection of object and put it into an Iterator object that defines how to traverse these collection of objects.

Iterator pattern is widely used in Java Collection Framework. This pattern falls under behavioral design pattern.

Advantages of Iterator Pattern

  • Iterator design pattern hides the implementation logic of object traversal through the collection and client programs just use iterator methods. The logic for iteration is embedded in the collection itself and it helps client program to iterate over them easily.

  • Iterator pattern supports different kind of iterators based on our requirements as long as it implements iterator interface. For example, we can implement a custom iterator to traverse the objects of a collection in increasing order.

  • Iterator pattern de-couple the algorithm logic from the data structure. Suppose there is a method which calculates the sum of all numbers provided by an iterator object. It doesn't matter whether numbers are stored in linked list or binary tree as long as Iterator objects provides a way to traverse all numbers, the method can calculate the sum.

Implementation of Iterator Design Pattern

We will first create an Iterator interface which contains hasNext and next navigation method and a Iterable interface which retruns the reference to iterator object.

Iterator Design Pattern UML Diagram Iterator.java
  • hasNext() : Returns true if the iteration has more elements.
  • next(): Returns the next element in the iteration.
public interface Iterator {
    public boolean hasNext();
    public Object next();
}

Iterable.java
  • getIterator() : Returns an implementation of Iterator interface for traversal over.
public interface Iterable {
    public Iterator getIterator();
}

ProductCatalog.java

The ProductCatalog class contains a sting array storing product names will implement Iterable interface to provide an iterator object to traverse product catalog.The ProductCatalog class contains an inner class ProductIterator which implements Iterator interface for the traversal of product catalog. ProductIterator class contains the logic regarding how to traverse product catalog, which is hidden from the client.

public class ProductCatalog implements Iterable{
    public String productCatalog[] = {"Toothbrush","Soap",
        "Toothpaste","Sampoo","Handwash"};

    public Iterator getIterator() {
        return new ProductIterator();
    }

    private class ProductIterator implements Iterator {
        int index;
        @Override
        public boolean hasNext() {
            if(index < productCatalog.length){
                return true;
            }
            return false;
        }

        @Override
        public Object next() {
            if(this.hasNext()){
                return productCatalog[index++];
            }
            return null;
        }
    }
}

IteratorPatternExample.java
public class IteratorPatternExample {
    public static void main(String[] args) {
        ProductCatalog productCatalog = new ProductCatalog();
        Iterator iterator = productCatalog.getIterator();
        while(iterator.hasNext()) {
            System.out.println("Product Name : " + iterator.next());
        }
    }
}

Output

Product Name : Toothbrush
Product Name : Soap
Product Name : Toothpaste
Product Name : Sampoo
Product Name : Handwash

Related Topics
Abstract Factory Design Pattern
Builder Design Pattern
Strategy Design Pattern
State Design Pattern
Flyweight Design Pattern
Composite Design Pattern
Singleton Design Pattern
Bridge Design Pattern
Adapter Design Pattern
Command Design Pattern
Filter Design Pattern