Iterator Design Pattern


Intent

It is belongs to behavioral design patterns catalog.
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Also known as

Cursor

Advantage of Iterator Pattern

  • It supports variations in the traversal of a collection.
  • It simplifies the interface to the collection.

Structure


Participants

1. Iterator
  • defines an interface for accessing and traversing elements.
2. ConcreteIterator
  •  implements the Iterator interface. 
  •  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 Iterator creation interface to return an instanceof the proper ConcreteIterator.

Collaborations

  •  A ConcreteIterator keeps track of the current object in the aggregate and can compute the succeeding object in the traversal. 

Source code

Step 1: Create a Iterator interface.
/**
 * 
 * ItemIterator interface.
 * 
 */
public interface ItemIterator {

  boolean hasNext();

  Item next();
}
Step 2: Create Item class (iterating list of items).
public class Item {

  private ItemType type;
  private String name;

  public Item(ItemType type, String name) {
    this.setType(type);
    this.name = name;
  }

  @Override
  public String toString() {
    return name;
  }

  public ItemType getType() {
    return type;
  }

  public final void setType(ItemType type) {
    this.type = type;
  }
}
Step 3 :  Create enum for ItemTypes
public enum ItemType {

  ANY, WEAPON, RING, POTION

}
Step 4: Create TreasureChest class which holds a collection of items.
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * TreasureChest, the collection class.
 * 
 */
public class TreasureChest {

  private List<Item> items;

  /**
   * Constructor
   */
  public TreasureChest() {
    items = new ArrayList<>();
    items.add(new Item(ItemType.POTION, "Potion of courage"));
    items.add(new Item(ItemType.RING, "Ring of shadows"));
    items.add(new Item(ItemType.POTION, "Potion of wisdom"));
    items.add(new Item(ItemType.POTION, "Potion of blood"));
    items.add(new Item(ItemType.WEAPON, "Sword of silver +1"));
    items.add(new Item(ItemType.POTION, "Potion of rust"));
    items.add(new Item(ItemType.POTION, "Potion of healing"));
    items.add(new Item(ItemType.RING, "Ring of armor"));
    items.add(new Item(ItemType.WEAPON, "Steel halberd"));
    items.add(new Item(ItemType.WEAPON, "Dagger of poison"));
  }

  ItemIterator iterator(ItemType itemType) {
    return new TreasureChestItemIterator(this, itemType);
  }

  /**
   * Get all items
   */
  public List<Item> getItems() {
    List<Item> list = new ArrayList<>();
    list.addAll(items);
    return list;
  }

}
Step 5: Let's create an Iterator implementation class TreasureChestItemIterator.java
import java.util.List;

/**
 * 
 * TreasureChestItemIterator
 *
 */
public class TreasureChestItemIterator implements ItemIterator {

  private TreasureChest chest;
  private int idx;
  private ItemType type;

  /**
   * Constructor
   */
  public TreasureChestItemIterator(TreasureChest chest, ItemType type) {
    this.chest = chest;
    this.type = type;
    this.idx = -1;
  }

  @Override
  public boolean hasNext() {
    return findNextIdx() != -1;
  }

  @Override
  public Item next() {
    idx = findNextIdx();
    if (idx != -1) {
      return chest.getItems().get(idx);
    }
    return null;
  }

  private int findNextIdx() {

    List<Item> items = chest.getItems();
    boolean found = false;
    int tempIdx = idx;
    while (!found) {
      tempIdx++;
      if (tempIdx >= items.size()) {
        tempIdx = -1;
        break;
      }
      if (type.equals(ItemType.ANY) || items.get(tempIdx).getType().equals(type)) {
        break;
      }
    }
    return tempIdx;
  }
}
Step 6: Create App class to test Iterator design pattern.
public class App {

  /**
   * Program entry point
   * 
   * @param args command line args
   */
  public static void main(String[] args) {
    TreasureChest chest = new TreasureChest();

    ItemIterator ringIterator = chest.iterator(ItemType.RING);
    while (ringIterator.hasNext()) {
      System.out.println(ringIterator.next());
    }

    System.out.println("----------");

    ItemIterator potionIterator = chest.iterator(ItemType.POTION);
    while (potionIterator.hasNext()) {
      System.out.println(potionIterator.next());
    }

    System.out.println("----------");

    ItemIterator weaponIterator = chest.iterator(ItemType.WEAPON);
    while (weaponIterator.hasNext()) {
      System.out.println(weaponIterator.next());
    }

    System.out.println("----------");

    ItemIterator it = chest.iterator(ItemType.ANY);
    while (it.hasNext()) {
      System.out.println(it.next());
    }
  }
}

Applicability

Use the Iterator pattern
  • to access an aggregate object's contents without exposing its internal representation
  • to support multiple traversals of aggregate objects
  • to provide a uniform interface for traversing different aggregate structures

Real world examples