Alexis Lopez

About Alexis Lopez

Java Lover, certified as Java Programmer, Mobile Application Developer and Web Component Developer.

Iterator Pattern and Java

Hello all, in this post we’ll be checking on the Iterator Pattern. A design pattern that I know many of you have already used, but maybe you didn’t realize it was pattern or didn’t know its great value. According to the book Head First Design:

The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlaying representation.

Whaaaaat? Well, it says that no matter what data structure (arrays, lists, hashtables,etc.) you are using, you can always traverse it in the same way if you implement this pattern. It gives you a uniform way of accessing the elements of your data structures (aggregates), but you don’t have to know what kind of data structure
 
you are traversing… nice! Also, it sets the responsibility of iteration on the Iterator object not on your data structure, which simplifies the coding in your data structure. Let’s check the classic class diagram for the Iterator pattern:

The actual class diagram for the Iterator Pattern has a few changes, specially in the Iterator class (interface), where we now have different methods as we’ll see in a minute, but first, lets review each of the previous classes (or interfaces):

  • Aggregate: This is the base class (or interface) of our data structures, you can think of it as the java.util.Collection interface, which defines a lot of methods for the collection classes.
  • ConcreteAggregate: This is the concrete data structure that we’ll be iterating, for example, java.util.ArrayList, java.util.Vector, etc.
  • Iterator: Base class (or interface) for iterators. You can find one in Java at java.util.Iterator. You can notice that the Java version has different methods that we’ll be discussing later in this post. Here you define de methods you need in order to traverse the data structures.
  • ConcreteIterator: As you want to traverse different data structures you need different iterators. So a concreteIterator is an Iterator for the data structure you want to traverse.

Now, let’s take a look at the Java implementation of the Iterator Pattern. The following diagram was generated using Architexa’s Free Tool for Understanding Code and it shows the relations between some classes of the Java Collections Framework where we can see a structure similar to the classic class diagram:

The above diagram shows only one implementation of the pattern in Java, there are a lot more, but they always use the java.util.Iterator interface; this is the interface you should use in your implementations of the Iterator Pattern when coding in Java. Let’s compare both diagrams:

Classic Diagram
ConcreteIterator

Notice that the methods of the Iterator object in the Java example are different from the methods in the classic class diagram:

  • There is no +First() method. If you need to go to the first element, you have to instance a new iterator.
  • The +IsDone() method has been renamed to +hasNext().
  • The +Next() and +CurrentItem() has been merged into +next().
  • The +remove() method has been added.

So, if you ever have to work with different data structures and need a uniform way to traverse them and/or access their items, think about the Iterator Pattern:

//... in a class
  /**
   * Traverse a list, hashtable, vector, etc. what ever that implements
   * the Iterator Pattern
   */
  public void traverse(Iterator iter)
  {
     while(iter.hasNext())
     {
 System.out.println(iter.next());
     }
  }

Of course, you will always have to create the ConcreteIterator class for your data structure, but if you are using classes from the
Java Collections Framework, it’s already done.

One last thing, remember the most important OO Principle of all: Always use the simplest solution that meets your needs, even if it doesn’t include a pattern.

Resources:

Freeman Eric and Freeman Elisabeth and Sierra Kathy and Bates Bert (2004). Head First Design Patterns. United States of America: O’Reilly Media, Inc.
 

Reference: Iterator Pattern and Java from our JCG partner Alexis Lopez at the Java and ME blog.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


× 1 = one



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books