Java Reference
In-Depth Information
public
public void
void actionPerformed ( ActionEvent );
We'll see examples of using lambdas for GUI action listeners in Chapter 14 . Here's one
single example to whet your appetite:
quitButton.addActionListener(e -> System.exit(0));
Because not everybody writes GUI applications these days, let's start with an example that
doesn't require GUI programming. Suppose we have a collection of camera model descriptor
objects that has already been loaded from a database into memory , and we want to write a
general-purpose API for searching them, for use by other parts of our application.
The first thought might be along the following lines:
public
public interface
interface CameraInfo
CameraInfo {
public
public List < Camera > findByMake ();
public
public List < Camera > findByModel ();
...
}
Perhaps you can already see the problem. You will also need to write a findByPrice() ,
findByMakeAndModel() , findByYearIntroduced() , and so on, as your application grows
in complexity.
You could consider implementing a “query by example” method, where you pass in a Cam-
era object and all its nonnull fields are used in the comparison. But then how would you im-
plement finding cameras with interchangeable lenses under $500 ? [ 32 ]
So a better approach is probably to use a “callback function” to do the comparison. Then you
can provide an anonymous inner class to do any kind of searching you need. You'd want to
be able to write callback methods like this:
public
public boolean
boolean choose ( Camera c ) {
return
return c . isIlc () && c . getPrice () < 500 ;
}
Accordingly, we'll build that into an interface: [ 33 ]
 
 
Search WWH ::




Custom Search