Design Patterns Java™ Workbook

(Michael S) #1
Chapter 28. Iterator

protected static ProcessStep inspect()
{
return new ProcessStep("Inspect");
}


protected static ProcessAlternation reworkOrFinish()
{
return new ProcessAlternation(
"Rework inner shell, or complete shell",
new ProcessComponent[] { rework(), finish()});
}


protected static ProcessSequence rework()
{
return new ProcessSequence(
"Rework",
new ProcessComponent[] { disassemble(), make()});
}


protected static ProcessStep disassemble()
{
return new ProcessStep("Disassemble");
}


protected static ProcessStep finish()
{
return new ProcessStep(
"Finish: Attach lift, insert fusing, wrap");
}
}


Suppose that you want to provide an iterator for the ProcessComponent hierarchy.
Mechanics for iterating over a composite node will differ from those for iterating over a leaf
node. These two types of iterators will presumably have some behavior or attributes in
common, so you can create a hierarchy of these iterators, such as the classes that Figure 28.4
shows. The classes in Figure 28.4 provide the hasNext() and next() methods that you
might expect from an iterator, as well as a depth() method that will tell the current depth of
an iterator within a composite.

Free download pdf