The iterator pattern

The purpose of the iterator pattern is to separate the logic of an algorithm manipulating the data from the particular structure of the container containing the data [GHJ95].

As an example, let us consider the problem of obtaining weather data for a simulation model. As previously mentioned, different authors have solved this problem using different mechanisms; some authors read the weather data from a text file saved locally in the system [HWH01]. Others have developed complex systems to obtain weather data from networks of real-time weather stations [LKN02].

Algorithms used in both cases have things in common and things that are different. The things in common are that in both cases an iteration is used to sequentially analyze each of the daily (or other time unit used) data. Things that are different are the particular data structures or data containers used in each of the cases. In the case that data are saved in a text file, the data container is a file containing lines of data, each line containing weather data for a day or other time unit used in the simulation. At each step of the iteration, a line containing the daily data will be read and the corresponding values will be assigned to variables designed to hold them. In the case that weather data are obtained from a weather station, the data container can be a table that is returned from the execution of an SQL statement. Each row of the table represents daily data. Looping through the container, an object holding the daily weather data will be returned and each of the daily weather parameters can be obtained by sending the appropriate message to this object. As an example, to get the rainfall for the day, the message getRainfall should be sent to the object holding the daily data.

It is desirable to design the Weather object in a general way that multiple sources could be used and independently of the particular container used to hold the data. This problem can be solved using the Iterator pattern. Figure 716 shows a class diagram for classes involved in the Iterator pattern.

The class diagram shown in this figure is taken from a crop simulation scenario. Object Simulator needs daily weather data for the simulation process. The Simulator has access to Weatherlnterface that defines the operations needed to obtain the weather data. Weatherlnterface implements interface Iterator provided by the Java programming environment that makes available the logic for iterating over a data container. Therefore,

Weatherlnterface defines iterator behavior as well. Class Weather implements the Weatherlnterface; therefore, it will provide the behavior necessary for obtaining the data from a particular data container. Class Weather has access to WeatherDataContainer from where it will extract the data. WeatherDataContainer is a collection of Daily WeatherData that is an object with attributes such as rainfall, minTemperature, maxTemperature, solarRadiation, etc. The Simulator has access to an object of type DailyWeatherData and can obtain the values of rainfall, temperature and solar radiation by sending to this object messages such as getRainFall, getSolarRadiation, etc.

It is important to note that the Simulator that requests the data is completely independent of the class Weather that provides them. As Simulator has access to the interface Weatherlnterface, not to the concrete class Weather, it can use any Weather object that provides a polymorphic implementation of this interface. Hence, this architecture has two advantages: First decouples Simulator from the concrete class Weather and second, it makes the algorithm that uses the weather data independent from the particular data container. Therefore, different sources of weather data can be used in the simulation process.

Figure 7-16. Classes involved in the iterator pattern.

Figure 7-17 shows another way of imposing class Weather to provide Iterator type of behavior. Class Weather implements two interfaces and will provide the behavior defined in both interfaces.

Figure 7-17. Another class diagram for Iterator.

A detailed implementation of this pattern in Java is provided in section Implementation of the Kraalingen model in Java of Chapter 8.

0 0

Post a comment