Java Reference
In-Depth Information
public void visit(Machine m)
{
??
}
public void visit(MachineComposite mc)
{
??
}
}
CHALLENGE 29.3
Complete the code of the RakeVisitor class to collect the leaves of a machine
component.
A short program can find the leaves of a machine component and print them out:
package com.oozinoz.applications;
import com.oozinoz.machine.*;
import java.io.*;
import com.oozinoz.io.WrapFilter;
import com.oozinoz.dublin.RakeVisitor;
public class ShowRake
{
public static void main(String[] args)
throws IOException
{
MachineComponent f = OozinozFactory.dublin();
Writer out = new PrintWriter(System.out);
out = new WrapFilter(new BufferedWriter(out), 60);
out.write(
new RakeVisitor().getLeaves(f).toString());
out.close();
}
}
This program uses a wrap filter to produce the output:
[Mixer1201, StarPress3401, Mixer2202, StarPress3404,
StarPress1401, Mixer3202, StarPress2402, Fuser1101,
ShellAssembler3302, Mixer3204, Mixer2201, Fuser2101,
StarPress3403, Fuser3102, Mixer3201, StarPress3402,
StarPress2401, ShellAssembler3301, ShellAssembler1301,
Mixer3203, ShellAssembler2301, Fuser3101]
The FindVisitor and RakeVisitor classes each add a new behavior to
the MachineComponent hierarchy, and these classes appear to work correctly. However,
a danger in writing visitors is that they require an understanding of the hierarchy that you are
extending. A change in the hierarchy may break your visitor, and you may misunderstand
the mechanics of the hierarchy initially. In particular, you may have to handle cycles if
the composite you are visiting does not prevent them.
Search WWH ::




Custom Search