I have a code that looks like this:
public interface BaseDAO{
}
public interface CustomerDAO extends BaseDAO{
public void createCustomer();
public void deleteCustomer();
public Customer getCustomer(int id);
}
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();
}
public class ProdDAOFactory extends DAOFactory {
public BaseDAO getCustomerDAO() {
return new ProdCustomerDAO();
}
}
Now I know that this code smells .. for many reasons. However, this code is also here:
http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html , see 9.8
I intend to do the following: 1) Switch my DAO implementations at runtime based on the environment (system properties). 2) Use java generics so that I can avoid type casting ... for example, does something like this:
CustomerDAO dao = factory.getCustomerDAO();
dao.getCustomer();
Unlike:
CustomerDAO dao = (CustomerDAO) factory.getCustomerDAO();
dao.getCustomer();
Your thoughts and suggestions, please.
source
share