Java Reference
In-Depth Information
Figure 28.5. You can extend the
ProcessComponent
hierarchy to implement an Iterable
interface, allowing
CompositeIterator
to build new iterators for each child of
a composite node.
The
ProcessComposite
class can implement
iterator()
to return an instance of
CompositeIterator
. The
ProcessStep
class can implement this method to return an
instance of
LeafIterator
.
CHALLENGE 28.2
What pattern are you applying if you let classes in the
ProcessComponent
hierarchy implement
iterator()
to create instances of an appropriate iterator
class?
The
ComponentIterator
hierarchy is somewhat difficult to develop, but it is highly
reusable and easy to apply. For example, a short program can print the nodes of a composite,
using indentation to show each node's depth: