Java Reference
In-Depth Information
CHALLENGE 5.4
What does the following program print out?
package com.oozinoz.machine;
public class ShowPlant
{
public static void main(String[] args)
{
MachineComponent c = OozinozFactory.plant();
System.out.println(c.getMachineCount());
}
}
The GUI application that Oozinoz uses to let engineers build object models of a factory's
machinery should check whether a node already exists in a component tree before adding it
a second time. A simple way to do this is to maintain a set of existing nodes. You may not,
however, always have control over how a composite is formed. In this case, you can write
an isTree() method to check whether a composite is a tree.
An object model is a tree if an algorithm can traverse its references without encountering
the same node twice. You can implement an isTree() method on the abstract class
MachineComponent so that it delegates to an isTree() method that maintains a set of
visited nodes. The MachineComponent class can leave the implementation of
the parameterized isTree(s:Set) method abstract. Figure 5.5 shows the placement of
the isTree() methods.
Figure 5.5. An isTree() method can detect whether a composite is in fact a tree.
Search WWH ::




Custom Search