Summary


Adapters

I have chosen to discuss adapters at the end of the chapter, not because they are advanced concepts, but because they are convenience classes doing nothing that couldn’t be done by implementing an EventListener interface. Sometimes the situation arises where you need to implement an EventListener interface but are only interested in some of its methods. The Java Language specification of course, does not allow “partial” implementation of interfaces except by abstract classes, so to fulfill the implementation contract, you would have to provide do-nothing implementations for any methods you weren’t interested in. An adapter class implements all an interface’s methods with do-nothing methods thereby providing you the option of extending it and overriding only the methods in which you are interested. As a concrete example of an adapter, let’s look at java.event.MouseAdapter, the source code of which is something like that shown in example 13.11.

Example 13.11: java.awt.event.MouseAdapter.java (extrapolated from Sun’s source code)

image from book
 1     package java.awt.event; 2 3     import java.awt.event.MouseEvent; 4     import java.awt.event.MouseListener; 5 6     public abstract class MouseAdapter implements MouseListener { 7       public void mouseClicked(MouseEvent e) {} 8       public void mousePressed(MouseEvent e) {} 9       public void mouseReleased(MouseEvent e) {} 10      public void mouseEntered(MouseEvent e) {} 11      public void mouseExited(MouseEvent e) {} 12    }
image from book

Now, consider the case where you need to create a MouseListener, but are only interested in handling the MOUSE_CLICKED event. Below, the MouseClickImplementer class uses the traditional approach of implementing MouseListener while the MouseClickExtender class extends from MouseAdapter.

Example 13.12: MouseClickImplementer.java

image from book
 1     package chap13; 2 3     import java.awt.event.MouseEvent; 4     import java.awt.event.MouseListener; 5 6     public class MouseClickImplementer implements MouseListener { 7       public void mouseClicked(MouseEvent e) { 8         System.out.println("mouseClicked"); 9       } 10      public void mousePressed(MouseEvent e) {} 11      public void mouseReleased(MouseEvent e) {} 12      public void mouseEntered(MouseEvent e) {} 13      public void mouseExited(MouseEvent e) {} 14    }
image from book

Example 13.13: MouseClickExtender.java

image from book
 1     package chap13; 2 3     import java.awt.event.MouseAdapter; 4     import java.awt.event.MouseEvent; 5 6     public class MouseClickExtender extends MouseAdapter { 7       public void mouseClicked(MouseEvent e) { 8         System.out.println("mouseClicked"); 9       } 10    }
image from book

As you can see, MouseClickExtender is slightly more concise. I find adapters to be useful mostly when I am creating anonymous listener instances and want to keep my code succinct for readability purposes, as in:

 myComponent.addMouseListener(new MouseAdapter() {       public void mouseClicked(MouseEvent e) {             System.out.println("mouseClicked");       } });




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net