Design Patterns Java™ Workbook

(Michael S) #1
Appendix B. Solutions

SOLUTION 3.5.......................................................................................................................................

If you have the opportunity, try to convince a colleague of your view.



  • Sample argument: The intent of the Adapter pattern is to convert the interface of
    a class into another interface that clients expect. The MessageAdapter class
    achieves this, letting a TextArea object pose as a PrintStream object. Thus,
    MessageAdapter is a valid, if dangerous, example of Adapter.

  • On the other side: The MessageAdapter class is simply delegating a call to
    a PrintStream object. We don't want to consider that every instance of delegation
    is an instance of Adapter. The Adapter pattern's intent is to translate an interface. This
    example has no interface, just a particular method call that we need to override.


If you can clearly articulate your own views and listen thoughtfully to opposition at the same
time, you win.


SOLUTION 3.6.......................................................................................................................................


  • One argument: When the user clicks the mouse, I need to translate, or adapt, the
    resulting Swing call into an appropriate action. In other words, when I need to adapt
    GUI events to my application's interface, I use Swing adapter classes. I am translating
    from one interface to another, fulfilling the intent of the Adapter pattern.

  • A counterargument: The "adapter" classes in Swing are stubs. They don't translate or
    adapt anything. You subclass these classes, overriding the methods you need to do
    something. It is thus your methods and your class that form an example of ADAPTER.

Free download pdf