Java Reference
In-Depth Information
Figure 3.8. A
MessageAdapter
object is a
PrintStream
object that forwards messages
to a
JTextArea
object's
append()
method.
Suppose that you implement this design and pass a
MessageAdapter
object to
the application's
setOutput()
method. Although you may find that all your tests pass, this
design is fragile.
CHALLENGE 3.3
Write down a comment explaining what might go wrong with this approach to
adaptation.
You could try making
MessageAdapter
more robust by overriding every method in
PrintStream
, but this is difficult and relies on your ability to translate every
PrintStream
behavior to a meaningful counterpart in
JTextArea
. A better solution
would specify the interface that the application can use. If you have access to the developers
of the client code, you can ask for support on their end.
CHALLENGE 3.4
You'd like to narrow the responsibility of the
MessageAdapter
class with
a specification of the calls the client might make to it. What request should you
make?
Recognizing Adapter
Adapters are healthier when a Java interface narrows the responsibility that the adapter class
bears to the client. The
JTable
class, for example, specifies the exact interface it needs with
the
TableModel
interface. By comparison, the
MessageAdapter
class has no contract