Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Ferret Interface

First, let's look at the interface itself. An interface is similar to a Java class, except that it doesn't do anything but define a series of methods (and perhaps member variables). In our case, we're dealing with the interface Ferret, in the org.chase.research package. The actual code is shown in Listing 1.







Listing 1 Ferret Interface

package org.chase.research;

public interface Ferret {

  public void find(String searchTerm);
  public int getNumResults();
  public String getResults();
  public String getFirstResult();

}

Notice that the interface doesn't tell us how to do anything; it just lists what must be done. A Ferret can do only four things: find information on a search term, return the number of results, return all of the available results, and return the first result. It's up to the implementing class to decide exactly how to do it.

This gives us a great deal of freedom because there's nothing about a Ferret that mandates a particular source, a method of finding results, or anything of the like. In this article, we're going to use Google, but you could write a Ferret that searches another search engine, an in-house database, or some completely different source. The beauty of it is that the interface just doesn't care.

  • + Share This
  • 🔖 Save To Your Account