Say I have a Java interface Blender.java with various implementations Cuisinart.java, Oster.java, Blendtec.java, etc. Now I want to write a program like so:
public class Blendifier {
// ...
public static void main(String... args) {
Blender blender = new Cuisinart();
blender.blend();
}
}
But now if I want to use the Blendtec instead of the Cuisinart, I have to open up the source, change the code, and recompile.
Instead, I'd like to be able to specify which Blender to use on the fly when I run the program, by writing the class name I want as a command line argument.
But how can I go from a String containing the name of a class, to constructing an actual instance of that class?
.propertiesconfig file to map permission names to implementing classes; my 'HtmlRenderer' looks in a configured package to see if there is a class<Something>Rendererfor any tag found in the input e.g.<pre>is found in input, it looks for a classPreRendererin the package given when the 'HtmlRenderer' is instantiated. No modifying code to add a newcaseorifwhen you add a new brand of blender.