Java Reference
In-Depth Information
package com.oozinoz.applications;
import com.oozinoz.process.*;
import com.oozinoz.util.*;
public class ShowProcessIteration
{
public static void main(String[] args)
{
ProcessComponent pc = ShellProcess.make();
ComponentIterator i = pc.iterator();
while (i.hasNext())
{
for (int j = 0; j < i.depth() * 4; j++)
{
System.out.print(' ');
}
System.out.println(i.next());
}
}
}
This program prints out:
Make an aerial shell
Build inner shell
Inspect
Rework inner shell, or complete shell
Rework
Disassemble
Finish: Attach lift, insert fusing, wrap
Iterating over a cyclic composite is not the best way to produce a textual description of
the composite. For a composite pretty-printer, see page 346 in Chapter 29, Visitor.
The printout here demonstrates that the code completes without entering an infinite loop,
a testament to the Component-Iterator hierarchy's ability to iterate over a cyclic
composite. The output also shows that the iterator correctly tracks the depth of each node.
To write the code for the composite iterator, you can start at the top of the hierarchy, with
the ComponentIterator class. This class defines the common operations for the hierarchy
and holds a node object that represents either a leaf or an interior node. For example, when
iterating over a process composite, the node object will be an instance of either
ProcessStep or ProcessComposite .
package com.oozinoz.util;
import java.util.*;
public abstract class ComponentIterator
{
protected Object node;
protected Set visited = new HashSet();
public ComponentIterator(Object node, Set visited)
{
this.node = node;
this.visited = visited;
}
Search WWH ::




Custom Search