public interface Aggregat
{
public Iterator createIterator();
}
public class ConcreteAggregat implements Aggregat
{
private Vector vector = null;
public Vector getVector()
{
return vector;
}
public void setVector(final Vector vector)
{
this.vector = vector;
}
public ConcreteAggregat()
{
vector = new Vector();
vector.add("vector 1");
vector.add("vector 2");
}
@Override
public Iterator createIterator()
{
return new ConcreteIterator(vector);
}
}
public class Client
{
public static void main(final String[] args)
{
final Aggregat agg = new ConcreteAggregat();
final Iterator iterator = agg.createIterator();
System.out.println(iterator.first());
while (!iterator.isDone())
{
System.out.println(iterator.next());
}
}
}
结果:
vector 1
vector 2
JDK也提供了迭代接口进行java collection的遍历:
Iterator it = list.iterator();