25 public static SimulationController getlnstance() {

26 if (uniquelnstance = null)

27 uniquelnstance = new SimulationControllerQ;

Figure 9-4. Java implementation of the plug and play SimulatorController (Part 2 of 4).

Line 2 imports a library that makes available the functionalities of class ResourceBoundle. The functionalities provided by this class are used to create classes from names read from the configuration file, as shown in lines 13 through 16. Lines 7 through 9 show that SimulationController has access to interfaces IPlant, ISoil, and IWeatherDataProvider. Line 13 defines a resource bundle, a Java artifact for implementing a configuration file that will hold the names of the classes that SimulationController needs to create. Figure 9-2, in the previous section, shows an implementation example of the configuration file. This file can be edited using any text editor. In this case, the SimulationController needs to create classes Plant, Soil, and WeatherDataFromFile. Line 13 obtains access to the configuration file referred to as ClassNames. Lines 14 through 16 create objects of type Weather, Plant, and Soil. As the process of creating objects using reflection is not a very straightforward one, let us take a closer look at this process. First, the name of the class is obtained as a string by sending to classBundle the message getString("className"). Thus, classBundle.getString ("weather") will return the name of class Weather. Second, (IWeather) ObjectFactory.newInstance(classBundle.getString( "weather")) will create an object of type IWeather. Note that ObjectFactory returns an object that is casted to become an instance of type IWeather. The same process is used to obtain objects of types Plant and Soil. Lines 17 through 74 are the same as in the previous versions of SimulationController class.

0 0

Post a comment