Java Reference
In-Depth Information
Figure 28.4. The ComponentIterator hierarchy anticipates different behavior for
iterating over composites and leaves.
The CompositeIterator class needs to create new "subiterators" as it traverses over the
children of a composite object. For each child, a CompositeIterator object must create
either a new leaf node iterator or a new composite iterator, depending on the type of the child.
This is an opportunity to apply polymorphism, letting the nodes decide which type of iterator
to instantiate. If you have access to the collection classes that you want to iterate over, you
can add an iterator() operation and let leaf and composite classes implement this
operation differently. If you introduce an Iterable interface, such as Figure 28.5 shows, the
Composite-Iterator class can call the iterator() method for each of its children.
The iterator() method produces an iterator that the composite must exhaust before
getting an iterator for the next child.
Search WWH ::

Custom Search