Best method to create a new instance based on precondition
<p>Hi my question is this. Suppose you have an interface that defines how a converter would be implemented. This interface could have a method loadDocument() and another convert(). Now suppose we want to have multiple converters, ConvertHTML2DOC, ConvertXML2HTML e.t.c you get the idea.</p>

<p>Suppose you get the two formats you need to convert in 2 strings (doesn't matter HOW you get them). Now you need to create an instance of your converter and convert the documents.</p>

<p>My question is which is better: to actually create an elaborate if statement or to load the classes through reflection with the Class class? to demonstrate I wrote a little example of what I mean. In my example I have 2 classes P1 and P2 that share an interface. I also create 50000 of them to show of the speed differences. As it turns out the normal invocation is slightly faster, but I think that in a complex example such as the one i mentioned in the beginning, the benefits of creating the classes through the Class method is more convenient and more maintainable. What are your thoughts?</p>

<pre><code>import java.util.*;

public class Test {
  public static void main(String[] args) {
    try {
      Random r = new Random();
      Date test1start = new Date();
      for (int i = 0; i&lt;= 50000; i++){
        Printable p = (Printable)Class.forName("P"+(r.nextInt(2)+1)).newInstance();
        System.out.println(p.getString());
      }
      Date test1stop = new Date();
      Date test2start = new Date();
      for (int i = 0; i&lt;= 50000; i++){
        Printable p;
        if (r.nextInt(2) == 0 ) {
           p = new P1();
        } else {
          p = new P2();
        }
        System.out.println(p.getString());
      }
      Date test2stop = new Date();
      System.out.println("Test with 'Class' invocation took "+(test1stop.getTime()-test1start.getTime())+" milliseconds.");
      System.out.println("Test with 'normal' invocation took "+(test2stop.getTime()-test2start.getTime())+" milliseconds.");
    } catch (Exception e) {
    }
  }
}

interface Printable {
  public String getString();
}

class P1 implements Printable {
  public String getString(){
    return "1";
  }
}

class P2 implements Printable {
  public String getString(){
    return "2";
}
  }
</code></pre>
