Previous Up Next

Part II
 

Configuration-driven Object Creation
 

Consider the configuration file below:

object_1 {
    type = "Person";
    name = "John Smith";
    age = "42";
}
object_2 {
    type = "Car";
    model = "Porsche 996 GT3";
    registration_number =  "R13 MEW";
}

It is possible to imagine an application that parses the above configuration file and iterates over all the object_<int> scopes. For each scope, the application creates an object of the type specified by the type variable within the scope, and sets instance variables of the newly created object to the values specified by variables within the scope.

Some readers may question the value of an application written in such a way. After all, it is probably syntactically shorter to just hard-code the creation of objects into the application:

obj1 = new Person();
obj1.setName("John Smith");
obj1.setAge(42);

obj2 = new Car();
obj2.setModel("Porsche 996 GT3");
obj2.setRegistrationNumber("R13 MEW");

The code can be even more concise if the values for instance variables are passed as parameters to constructors:

obj1 = new Person("John Smith", 42);
obj2 = new Car("Porsche 996 GT3", "R13 MEW");

Despite the relative verbosity of using a configuration file to specify the creation and initialisation of objects, this technique can offer important benefits for some niche programming tasks. Part II of this manual explores issues associated with using this technique in Config4*-based applications.


Previous Up Next