Java Reference
In-Depth Information
public int getMachineCount()
return 1;
The MachineComposite class also correctly implements getMachineCount() ,
returning the sum of the counts for each of a composite's components:
public int getMachineCount()
int count = 0;
Iterator i = components.iterator();
while (i.hasNext())
MachineComponent mc = (MachineComponent);
count += mc.getMachineCount();
return count;
These methods are correct so long as MachineComponent objects are trees. It can happen,
though, that a composite that you suppose is a tree suddenly becomes not a tree. This is
especially likely to occur when users can edit the composition. Consider an example that
might occur at Oozinoz.
The fireworks engineers at Oozinoz use a GUI application to record and to update
the composition of machinery in the factory. One day, they report a defect regarding the
number of machines that are reported to exist in the factory. You reproduce their object model
with a plant() method on an OozinozFactory class:
public static MachineComposite plant()
MachineComposite plant = new MachineComposite(100);
MachineComposite bay = new MachineComposite(101);
Machine m = new Mixer(102);
Machine n = new Mixer(103);
Machine o = new Mixer(104);
return plant;
This code produces the plant object shown in Figure 5.4.
Search WWH ::

Custom Search