Java Reference
In-Depth Information
dischargePaste();
flush();
}
Using the previous version of the AsterStarPress class, you created a subclass that
handled the effects of a shutdown, leaving a mold incomplete:
public class OzAsterStarPress extends AsterStarPress
{
public MaterialManager getManager()
{
return MaterialManager.getManager();
}
public void markMoldIncomplete(int id)
{
getManager().setMoldIncomplete(id);
}
}
You no longer need the OzAsterStarPress class, but you do need to set the command
hook in the AsterStarPress class.
CHALLENGE 24.4
Write an expression that creates an object that you can pass to
setShutdownHook() and that will notify the Oozinoz material manager of
the mold's state.
The execute() command of Hook accepts an instance of AsterStarPress as
an argument. The idea here is that the developer of a hook—namely you—will need some
information about that state of the press when your hook is called. If the mark-mold-
incomplete hook were the only hook for star press processing, it would make more sense to
use the ID of the mold machine as the parameter of the execute() command. The chosen
design, however, anticipates that other hooks will come along. Passing the complete
AsterStarPress object is a hedge that makes it likely that future hooks can be added
without changing the Hook interface.
The C OMMAND pattern affords an alternative design to a T EMPLATE M ETHOD for hooks and
is similar in intent, or structure, to several other patterns. The C OMMAND pattern is similar to
A DAPTER , in that a class such as JMenuItem or JTable arranges to call a client's code at
the right moment. C OMMAND is also similar to I NTERPRETER ; Challenge 25.3 on page 268
will ask you to compare them. Finally, C OMMAND is similar to a pattern in which a client
knows when an action is required but doesn't know exactly which operation to call.
CHALLENGE 24.5
Which pattern addresses the situation in which a client knows when to create
an object but doesn't know which class to instantiate?
Search WWH ::




Custom Search