Java Reference
In-Depth Information
package com.oozinoz.aster.client;
import com.oozinoz.aster.*;
public class OzAsterStarPress extends AsterStarPress
{
public MaterialManager getManager()
{
return MaterialManager.getManager();
}
public void markMoldIncomplete(int id)
{
getManager().setMoldIncomplete(id);
}
}
Note that the work involved in completing a T
EMPLATE
M
ETHOD
is similar to the work in
supplying an A
DAPTER
. The difference between these patterns is that A
DAPTER
calls for you
to translate an interface, whereas
TEM-PLATE
M
ETHOD
calls for you to supply a step of
an algorithm.
SOLUTION 21.3
What you want is a hook. You might phrase your request something like this: "I wonder
whether you could be so kind as to add a call in your
shutDown()
method, after discharging
the paste and before flushing? If you call it something like
collectPaste()
, I can use it to
save the paste that we reuse here at Oozinoz."
The developers are likely to negotiate with you about the name of the method. The point is
that by requesting a hook in a T
EMPLATE
M
ETHOD
, you can make your code much stronger
than you can by working around an inadequacy in the existing code.
SOLUTION 21.4
The
getPlanner()
method in the
Machine
class should take advantage of the abstract
createPlanner()
method:
public MachinePlanner getPlanner()
{
if (planner == null)
{
planner = createPlanner();
}
return planner;
}
After adding this method, you can delete the
getPlanner()
methods in the subclasses of
Machine
.
This refactoring creates an instance of T
EMPLATE
M
ETHOD
. The
get-Plan
ner() method
lazy-initializes the planner variable, relying on the
createPlanner()
step that subclasses
must supply.