4 public interface IWeather extends Iterator {

5 public double getSolarRadiation();

6 public double getAverageTempDuringDay();

7 public double getAverageTemperature();

8 public double getAverageTemperatureForPT();

9 public double getRainFall();

10 public double getTemperatureMin();

11 public double getTemperatureMax();

12 public double getPAR();

13 public int getDayOfYear();

14 public void initialize(Properties props);

Figure 8-32. Definition of interface IWeather in Java.

IWeather inherits behavior from another interface, the Iterator interface. Iterator is a pattern and the reasons of using this pattern are introduced in Chapter 7, Design Patterns in Part One of the book. Because IWeather inherits from Iterator, the behavior defined in Iterator will be part of the definition of IWeather as well. Figure 8-32 shows only the methods defined in interface IWeather:; methods inherited from Iterator are not shown. Any class Weather that will be considered for use in the system should implement IWeather and Iterator interfaces, as shown in Figure 8-33.

Figure 8-33. Class Weather implements the behavior defined in IWeather and Iterator.

The Iterator interface defines the behavior needed to loop over a container of data in order to analyze each of them. The method hasNextQ is used to assure that the iteration over the data continues as long as there are valid data in the container. The method nextQ gives access to the next data. In the case that there are no available data, nextQ will change the status of hasNextQ to false and the iteration will stop. The removeQ method removes an element from the container. This method is not used in our example.

Note that different classes implementing interface IWeather will provide different implementations for each of the methods defined in interface Iterator. The particular implementation each class will provide will be based on the kind of the container used to hold the data. In the case that the weather data are saved locally in a text file, the data container is a file containing lines and each line contains weather data for a day or other time unit used in the simulation. In the case that weather data are obtained from an on-line weather station, the container is a table and each row of the table contains the weather data for a day or other time unit used in the simulation.

In the next sections, we will show two Weather classes providing different implementations of the same set of Iterator and IWeather interfaces. The classes are WeatherDataFromFile and WeatherD at aFromStation, as described in Section 6 of Chapter 3, Interfaces, in the first part of the book.

10.3.1 Class WeatherDataFromFile

This class implements the behavior defined in IWeather interface in the case that the weather data are read from a local file in the system. Section 4.3 of Chapter 7, The Iterator Pattern, presents classes that are involved in this pattern and their collaborations. A class referred to as Daily Weather Data is designed to hold daily weather data such as solar radiation, minimum and maximum temperature, and rainfall. Note that the simulation unit is the day; an instance of class Daily WeatherData contains weather data for a particular day of the year. Figure 8-34 shows the implementation of this class in Java.

1 public class DailyWeatherData {

Was this article helpful?

0 0

Post a comment