title: Factory method pattern in java using generics, how to?
body: <p>I have code that looks like follows: </p>
  
  <pre><code>public interface BaseDAO{
  // marker interface
  }
  
  public interface CustomerDAO extends BaseDAO{
  public void createCustomer();
  public void deleteCustomer();
  public Customer getCustomer(int id);
  // etc
  }
  
  public abstract class DAOFactory {
  public BaseDAO getCustomerDAO();
  public static DAOFactory getInstance(){
    if(system.getProperty("allowtest").equals("yes")) {
    return new TestDAOFactory();
    }
    else return new ProdDAOFactory();
  }
  
  public class TestDAOFactory extends DAOFactory{
  public BaseDAO getCustomerDAO() {
    return new TestCustomerDAO(); // this is a concrete implementation
    //that extends CustomerDAO
    //and this implementation has dummy code on methods
  }
  
  public class ProdDAOFactory extends DAOFactory {
  public BaseDAO getCustomerDAO() {
    return new ProdCustomerDAO(); // this implementation would have 
    // code that would connect to the database and do some stuff..
  }
  }
  </code></pre>
  
  <p>Now, I do know that this code smells.. for many reasons. However, this code is here too: 
  <a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html" rel="noreferrer">http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html</a>, refer 9.8</p>
  
  <p>What I intend to do is this: 
  1) Switch my DAOs implementations at runtime based on environment (system properties).
  2) Make use of java generics so that I can avoid type casting... 
     for instance does something like this: </p>
  
  <pre><code>CustomerDAO dao = factory.getCustomerDAO();
  dao.getCustomer();
  </code></pre>
  
  <p>As opposed to: </p>
  
  <pre><code>CustomerDAO dao = (CustomerDAO) factory.getCustomerDAO();
  dao.getCustomer();
  </code></pre>
  
  <p>Your thoughts and suggestions, please.</p>