Unit02 DesignPatterns
Unit02 DesignPatterns
accept(visitor);
return "" + buffer;
}
// ...
}
The Iterator Pattern
• Like Visitor, an Iterator pattern provides a means to access one-
by-one, all the objects in a container.
• The following shows the Iterator interface:
public interface Iterator {
boolean hasNext ();
Object next () throws NoSuchElementException;
}
• An Iterator interacts closely with a container. Recall that a
container has a method iterator, which returns an Iterator.
• The following shows how Iterator interface is used:
• we downcast because we cant instantiate interfaces
• While the accept method takes only one visitor, a container can
The accept Method
We now define the accept method for the AbstractContainer class using
an iterator.