Java Design Patterns Iterator Patterns

Introduction

Iterator Patterns allow us to access the elements of an aggregate object sequentially without knowing its underlying structure.

Use case

  • Iterate through a list of element
  • Iterate through a tree structure

Example

import java.util.LinkedList;

interface Iterator {
   void first();// Reset to first element

   String next();// get next element

   Boolean IsDone();// End of collection check

   String CurrentItem();// Retrieve Current Item

}

interface Iterable {
   public Iterator CreateIterator();
}

class Array implements Iterable {
   private String[] subjects;

   public Array() {
      subjects = new String[2];
      subjects[0] = "array element 1";
      subjects[1] = "array element 2";
   }/*from  www.j  av  a 2 s  .  com*/

   public Iterator CreateIterator() {
      return new ArrayIterator(subjects);
   }

   // Containing the iterator
   public static class ArrayIterator implements Iterator {
      private String[] subjects;
      private int position;

      public ArrayIterator(String[] subjects) {
         this.subjects = subjects;
         position = 0;
      }

      public void first() {
         position = 0;
      }

      public String next() {
         return subjects[position++];
      }

      public Boolean IsDone() {
         return position >= subjects.length;
      }

      public String CurrentItem() {
         return subjects[position];
      }
   }

}

class List implements Iterable {
   private LinkedList<String> subjects;

   public List() {
      subjects = new LinkedList<String>();
      subjects.addLast("list element 1");
      subjects.addLast("list element 2");
      subjects.addLast("list element 3");
   }

   @Override
   public Iterator CreateIterator() {
      return new ListIterator(subjects);
   }

   // Containing the iterator
   public class ListIterator implements Iterator {
      private LinkedList<String> subjects;
      private int position;

      public ListIterator(LinkedList<String> subjects) {
         this.subjects = subjects;
         position = 0;
      }

      public void first() {
         position = 0;
      }

      public String next() {
         return subjects.get(position++);
      }

      public Boolean IsDone() {
         return position >= subjects.size();
      }

      public String CurrentItem() {
         return subjects.get(position);
      }
   }
}

public class Main {
   public static void main(String[] args) {
      Iterable myList = new List();
      Iterable myArray = new Array();

      Iterator listIterator = myList.CreateIterator();
      Iterator arrayIterator = myArray.CreateIterator();

      System.out.println("\nList subjects :");
      Print(listIterator);

      System.out.println("\nArray subjects :");
      Print(arrayIterator);
   }

   public static void Print(Iterator iterator) {
      while (!iterator.IsDone()) {
         System.out.println(iterator.next());
      }

   }
}



PreviousNext

Related