Java Reference
In-Depth Information
Figure 25.1. An interpreter hierarchy provides for runtime programming of a factory robot.
The interpreter hierarchy includes commands that let you start up, shut down, and unload
machines. (These commands require a bit of customization to work with our machinery.)
The interpreter hierarchy also provides a command that carries material from one machine to
another and provides
sequence
,
for
, and
if
commands that are compositions of other
commands.
The
ForMachines
class uses another command as the body of a
for
loop. This command
can be either a single command or a sequence embodied in a
CommandSequence
object.
The
IfCommand
class constructor takes a condition term, not shown, and two commands,
one of which it executes, depending on the condition. The
NullCommand
class implements
execute()
to do nothing. You can use this, for example, to nullify the
else
part of an
if
command.
Each class in the
Command
hierarchy is an interpreter. Polymorphism lets each class interpret
the
execute()
operation differently, in a way that matches the name of the class. You might
also say that each
instance
of a class in the hierarchy is an interpreter. The
CommandSequence
,
ForMachines
, and
IfCommand
classes let you compose new,
arbitrarily complex interpreters that can interpret the
execute()
command as a complete
program.