In my mind, this is the best use case you could imagine for a Visitor pattern.
Basically, there should be one class Parameters
that should hold all the parameters. Every object that needs one set of parameters can be visited with that Parameters
class. The object can then pass the parameters to its children that know which parameters to use and how.
In your case, this can be done this way:
public interface IParameterized{
public void processParameters(Parameters param);
}
public class Earth implements IParameterized{
public Earth(){
// Create all countries here and store them in a list or hashmap
}
public void processParameters(Parameters param){
// find the country you want and pass the parameters to it
country.processParameters(param);
}
}
public class Country implements IParameterized{
public Country(){
// Create all cities that belong to this country
}
public void processParameters(Parameters param){
// find the city you want and pass the parameters to it
city.processParameters(param);
}
}
public class City implements IParameterized{
public City(){
// Create city...
}
public void processParameters(Parameters param){
// Do something with the parameter
}
}
EDIT
To wire up the dots, this can be used in the following way:
public static void main(String... argv) {
Parameters params = new Parameters();
// Populate params from the command line parameters
Earth earth = new Earth();
// Earth takes the responsibilty of processing the parameters
// It will delegate the processing to the underlying objects in a chain
earth.processParameters(params);
}
As a side note, you could also have a look at the Chain Of Responsibility design pattern