Java Reference
In-Depth Information
public void execute(Context c)
{
Iterator i = c.machines.iterator();
while (i.hasNext())
{
Machine m = (Machine) i.next();
c.assign(variable, m);
body.execute(c);
}
}
This code iterates over the machines in the provided context, assigning a variable to each
machine and executing the loop's body. The idea here is for clients to create a body that
depends on the variable, as in a normal
for
loop. Consider a short Java program that shuts
down all the machines in the
MachineLine
context:
package com.oozinoz.robot.interpreter;
import com.oozinoz.machine.*;
public class ShowForCommand
{
public static void main(String[] args)
{
Context c = MachineLine.createContext();
Variable m = new Variable("m");
ForMachines fm =
new ForMachines(m, new ShutdownCommand(m));
fm.execute(c);
}
}
The body of the
for
command shuts down machine
"m"
. The
for
command assigns this
variable to each machine in the provided context and executes the body. The program
produces a log of the command execution:
Mixer1201 shutting down
Fuser1101 shutting down
StarPress1401 shutting down
ShellAssembler1301 shutting down
UnloadBuffer1501 shutting down
Implementors of the
Term
interface must implement
eval()
so that it returns an object of
type
Machine
. However, the
Term
hierarchy includes two classes that are effectively
Boolean operators that return
null
to mean false and return a
Machine
object to mean true.
Figure 25.4 shows the Boolean subclasses of
Term
.