Java Reference
In-Depth Information
The
Command
interpreter design uses a
Term
hierarchy to contain references to machines,
letting you create commands either for specific machines or by using variables. For example,
the
CarryCommand
class has a constructor that requires two
Term
objects, as Figure 25.2
shows.
Figure 25.2. Command classes, such as
CarryCommand
, use
Term
objects as
parameters. A
Context
object holds a mapping from machine and variable names to
Machine
objects.
The
Term
class defines an abstract
eval()
method that returns a machine and that
subclasses must implement. This provides flexibility to such classes as
CarryCommand
. If
you want to create a
CarryCommand
object that carries material between two specific
machines, you can use
Constant
objects to represent those machines. Alternatively, you can
create a
CarryCommand
object that will carry material between two variables, perhaps
fromMachine
and
toMachine
. You can assign those variables in any context, such as
within a
for
loop, reusing the
CarryCommand
object.
The
com.oozinoz.robot.interpreter
package includes, for demonstration purposes,
a
MachineLine
class. This class offers a static
createContext()
method that returns a
context with a mapping of machines in a particular line. The contents of this initial context
are: