| Classification
top
|
| Iterator is a behavioral pattern. Also known
as Cursor. Behavioural patterns are concerned with algorithms and the assignment
of responsibilities between objects, they describe patterns of objects or
classes and the patterns of communication between them." [Gamma, p.221] |
| |
| Intent
top
|
| The intent of this design pattern
is to provide a way to access the elements of an aggregate object sequentially
without exposing its underlying representation. In other words, an iterator
allows a client to access a contents of a certain data structure without
knowing about the internal representation of the contents. |
| |
| Motivation
top
|
| A data structure such as list should give the
user a way to access its elements in such a way that it does not expose its
internal structure. Also, you should be able to manipulate that list in different
ways, depending on what you want to accomplish, but at the same time you do
not want to all your manipulation procedures into the list interface even
if you can predict which ones you might need. The Iterator Pattern allows
you to do those things. The idea of this pattern is to take the responsibility
for access and manipulation of the data structure out of the list object and
put it into the iterator object. The iterator class will define the interface
for accessing the data structure's elements. This way we can define different
iterators for different ways of manipulating a data structure. |
| |
| Structure
top
|
|
Participants
- Iterator - defines an interface for accessing the
elements and traversing the elements
- ConcreteIterator - implements the Iterator interface
and keeps track of the current position in
the traversal of the aggregate
- Aggregate - defines an interface for creating an
Iterator object
- ConcreteAggregate - implements the Iterator creation
interface to return an instance of the
proper ConcreteIterator
|
| Consequences
top
|
There are three major consequences.
[Gamma, p. 260]
- Iterator Pattern supports variations in the traversal of
an aggregate. Every time you want to change the traversal algorithm, you
just replace the iterator instance with a different one.
- Iterators simplify the Aggregate interface.
- More than one traversal can be pending on an aggregate.
An iterator keeps track of its own traversal state. Therefore you can have
more than one traversal in progress at once.
|
| |
| Sample Code
top
|
| This sample code will show how Iterator
Pattern works. It is written in Java. In this example,
we will print out an array of numbers in two ways: 1) in the order
the numbers are in the array, 2) backwards. Here we have an
abstract aggregate class that creates an abstract iterator class.
Abstract iterator class defines the procedures for data
structures, such as "first", "next", "isDone",
and "CurrentItem". We, also, have two concrete aggregate
classes that create two concrete iterator classes. Those concrete
iterator classes have their own implementations of the procedures
specified in the abstract iterator.
aggregate.java
|
import java.util.Vector;
public abstract class aggregate {
public abstract iterator createiterator();
public void append(Object anObj){
elements.addElement(anObj);
}
public void remove(Object anObj){
elements.removeElement(anObj);
}
public Object currentItem(int n){
return elements.elementAt(n);
}
public int count(){
return elements.size();
}
private Vector elements = new Vector(5);
}
|