Java Reference
In-Depth Information
public int getRowCount()
{
return rockets.length;
}
public Object getValueAt(int row, int col)
{
switch (col)
{
case 0 :
return rockets[row].getName();
case 1 :
return new Double(rockets[row].getPrice());
case 2 :
return new Double(
rockets[row].getApogee().getMagnitude());
default :
return null;
}
}
}
The
TableModel
interface provides a good example of the power of planning ahead for
adaptation. This interface, along with its partial implementation by
AbstractTableModel
,
reduces the work of showing domain objects in a standard GUI table. Your solution should
show how easy it is to adapt when adaptation is supported with an interface.
SOLUTION 3.3
The problem with subclassing
PrintStream
and overriding only one of its many methods is
that in a later version, the client code might start using methods that you don't support. The
compiler won't detect a problem, because the client still needs a
PrintStream
object and
you will still be supplying one.
SOLUTION 3.4
You should ask the client code developers to specify a Java interface, such as:
public interface MessageChannel
{
void print(Object o);
}
The client developers also need to change the
setOutput()
method to accept an instance of
MessageChannel
instead of an instance of
Print-Stream
. In this scenario, you can
define
MessageAdapter
to subclass
Text-Area
and implement
MessageChannel
. The
advantage is that the client developers can't change the calls the application makes without
changing the interface. If the client developers change the interface, the worst possible result
is that your code won't compile. This is greatly preferable to the earlier design, in which a
change on the client side would not be noticeable until runtime.