Iterator Pattern

In my opnion, Iterator Pattern is probably one of the most powerful design patterns in the word of programming.

Why we need the Iterator Pattern?
1. It generalizes and simplifies the way of accessing elements.
2. We can represent a collection of objects with a iterator, but we don’t need to actually load it before we use it.

In Java, the Iterator interface is super simple:

public interface Iterator<E> {
  boolean hasNext();
  E next();
  void remove(); // Optional
}

If we need to iterate a List infinetely, what can we do?
Bad practice 1: Create a infinite List. (It’s insane!)

List<Integer> intList = new LinkedList<Integer>();
while(true) {
  intList.add(Math.random() * 100);
}

Bad practice 2: Loop a List infinetely. (Yes, it works, but you hava to know what you are gonna to do first and the application will be held.)

List<Integer> intList = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4));
while(true) {
  for(Integer i : intList) {
    // do somthing with i...
  }
}

Solution: How about we create an infinite Iterator called CycleIterator:

public class CycleIterator<E> implements Iterator<E> {

  private final Iterable<E> iterable;
  private Iterator<E> iterator;

  public CycleIterator(Iterable<E> iterable) {
    if (iterable == null)
      throw new NullPointerException();

    this.iterable = iterable;
    iterator = iterable.iterator();
  }

  @Override
  public boolean hasNext() {
    if (!iterator.hasNext())
      iterator = iterable.iterator();

    return iterator.hasNext();
  }

  @Override
  public E next() {
    if (!hasNext())
      throw new NoSuchElementException();

    return iterator.next();
  }

  @Override
  public void remove() {
    throw new UnsupportedOperationException();
  }

}

What we can do now?

List<Integer> intList = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4));
Iterator<Integer> cycleIter = new CycleIterator<Integer>(intList);
for(int i = 0; i <= 1000; i++)
  System.out.println(cycleIter.next());
...
for(int i = 0; i <= 10000; i++)
  System.out.println(cycleIter.next());
...
for(int i = 0; i <= 100000; i++)
  System.out.println(cycleIter.next());
...

Eventually, we can now iterate this infinity collection in any place of code we want.

Moreover, we can make it into an Iterable. (Note: An Iterable is simply an object which can provide an Iterator.)

public class CycleIterable<E> implements Iterable<E> {

  private Iterable<E> iterable;

  public CycleIterable(Iterable<E> iterable) {
    this.iterable = iterable;
  }

  @Override
  public Iterator<E> iterator() {
    return new CycleIterator<E>(iterable);
  }

}
Advertisements
This entry was posted in design pattern, java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s