O

Weatherlnterface

^getSolarRadiationf} : Double ^g et Ave ra g eTe m p e rat u re ( ) ; Double ^getRainfallt) : Double "^getTemperatureMini) • Double ^getTemperaturemaxQ ' Double ^getPARf) : Double ^getPotentialETQ : Double

Figure 3-4. UML symbols for an interface.

As shown in Figure 3-4, each of the symbols can be used as they represent the same thing, the graphic symbol of an interface. Figure 3-5 shows an example of interface defined in Java.

1 package Weather;

2 public interface Weatherlnterface {

3 public double getSolarRadiation();

4 public double getAverageTemperature();

5 public double getRainfall();

6 public double getTemperatureMin();

7 public double getTemperatureMax();

8 public double getPar();

9 public double getPotentialET();

Figure 3-5. Examples of interface definition in Java.

An interface only defines the kind of services an object should provide for use by other objects. It represents the set of messages that can be sent to an object created by a class that implements this interface. The implementing object will respond to any of the messages defined in the interface. The interface defines only the operations' signature composed of the operation name and the parameters required for its execution that must be conveyed in a message. There is no body in the methods or operations defined in the interface. The body for each of the methods will be provided by the class that implements this interface. Figure 3-6 shows an example of a class implementing an interface.

Figure 3-6. Class implementing an interface.

As shown in Figure 3-6, class Weather implements interface Weatherlnterface. This contractual agreement, establishes obligations in both sides: The interface defines what classes should at the least implement as functionality when binding the interface, and the classes realize that the interface definition is what they are expected to implement at the least. Therefore, the behavior of class Weather would include at the least the behavior defined by the interface that the class is implementing. Class Weather may have other methods that are not related to Weatherlnterface. An interface can be implemented by many classes; each of them would provide a particular implementation of the behavior defined by the interface. At the same time, a class may implement many interfaces, in which case the behavior of the class will include all the behaviors defined by the interfaces that the class implements. Figure 3-7 shows the example of class Weather implementing interface Weatherlnterface.

1 package Weather;

2 public class Weather implements Weatherlnterface {

3 private double solarRadiation;

4 private double rainFall;

5 private double potentialET;

6 private double temperatureMax;

7 private double temperatureMin;

8 private double par;

9 public double getSolarRadiation() {return solarRadiation;}

Figure 3-7. Class Weather implementing Weatherlnterface (Part 1 of 2).

10 public double getRainFall() {return rainFall;}

11 public double getTemperatureMax() {return temperatureMax;}

12 public double getTemperatureMin() {return temperatureMin;}

13 public double getAverageTemperature() {

14 return (getTemperatureMin()+getTemperatureMax())/2;

16 public double par(){return par;}

17 public double getPotentialET() {return potentialET;}

Figure 3-7. Class Weather implementing Weatherlnterface (Part 2 of 2).

As shown in Figure 3-7, the definition of class Weather includes all the operations defined in the interface Weatherlnterface. The interface defines only the signature for the operation, not the body, whereas the class definition includes the signature's operation and the code for implementing it. As an example, line 13 in Figure 3-7 is the same as line 4 in Figure 3-5, as they both define operation's signature. In Figure 3-7, line 14 provides the code implementation for the operation.

A better approach for the problem defined at the beginning of this section is to design an interface that would define the functionalities that classes implementing the interface should provide, with each of the classes providing a particular solution to the problem of obtaining the weather data. Figure 3-8 shows two classes implementing the same Weatherlnterface.

Weatherinteiface

^getSolarRadiationO - Doubl© ^getAverageTemperatureQ : Double *getRainfall() . Double ^getTemperatureMin(). Double ^getTemperaturemaxf). Double VtPARO : Double ^getPotentialETi). Double implements impiêrçients

WeatherDataFromStation

^getSolarRadiationQ : Double ^getAverageTemperatureO : Double ^getRainfallQ : Double ^getTemperatureMinO : Double ^getTemperalureMaxÖ : Double ^getPARO . Double ^getPctentialETQ . Double

WeatherData From F i le

^getSolarRadiation() • Double ^getAverageTemperatureO - Double ^getRainfallQ Double ^getTemperatureMin() ■ Double ^geîTemperatureMax() : Double ^getPARQ : Double ^getPotentialETO : Double

Figure 3-8. Example of two different classes implementing the same interface.

As shown in Figure 3-8, classes WeatherDataFromStation and WeatherDataFromFile implement the same interface Weather Interface. Both classes have agreed to provide behavior for the functionality defined in the interface. Class WeatherDataFromStation will provide the behavior necessary to obtain weather data from an online station. As such, this class is responsible for providing the logic for solving issues such as connecting to the station, making transactions, and downloading the data. Class WeatherDataFromFile will provide the behavior for reading the weather data from a text file. These two classes provide different behavior to implement the same functionality that is obtaining weather data. Different behaviors provide the same results, but in different ways. The same functionality is provided in many forms, and this is the meaning of polymorphism.

Interfaces are an elegant way of defining polymorphism without involving any implementation. Two classes provide polymorphic behavior if they implement the same set of interfaces. Interfaces formalize polymorphism.

The basic concept behind polymorphism is substitutability. Classes implementing the same interface can be a substitute to each other; any one of them can be used instead of the other. Therefore, interfaces are the heart of the "plug and play" architecture. Classes, components, or subsystems can substitute for each other provided that they implement the same set of interfaces. Interfaces are a key concept while developing component-based environmental and agricultural software [PBB04]. Software designs that do not use interfaces are rigid, and difficult to maintain and reuse. Interfaces allow objects to communicate with each other without demanding detailed knowledge of object's internal logic and implementation.

0 0

Post a comment