3.3. Service

A service does something useful and is implemented in the Java programming language. For example, a Web server is a service; so is the program that calculates taxes, the application that reports electricity consumption, and the security software that turns on the lights in an unoccupied home. In a business setting, a service may tell the temperature and merchandise quantity in vending machines; another may page the administrator when a critical connection to a customer's site goes down. The list of possible services can only be bound by one's imagination. It is important for us to demystify this much overloaded term and for you to understand its generality.

To develop a service, you usually define an interface that says what the service does, along with corresponding implementation classes that spell out how the service is to be performed. For example, the following is a service that plays digital music. First, we show its service interface:

package player.service;
// imports
public interface DigitalPlayer {
   /**
    * Plays music read from the stream.
    */
   public void play(InputStream musicStream);
}

The class that implements the interface may look like

package player.impl.mp3;
import player.service.DigitalPlayer;
// other imports
public class MP3Player implements DigitalPlayer {

   public void play(InputStream inStream) {
      // Read MP3 encoded bytes from the stream,
      // send to the appropriate codec
      // available in this service implementation.
   }
}

The specific code that reads, decodes, and plays MP3 is inconsequential to this discussion and is not shown in the play method.

The separation of interface and implementation ensures the service interface to the callers remains stable while the implementation undergoes changes. In our example, the code using the DigitalPlayer interface won't be affected if we substitute the MP3Player class with a RealAudioPlayer class, which would implement the same service interface but decipher the given stream in an entirely different format.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.186