pessoal to com uma duvida nesse padrão de projeto.
não to conseguindo entender a necessidade de junto com ele usar outro padrão chamado Abastract Factory, segue as classes de exemplo abaixo:
public class OracleDAOFactory extends DAOFactory {
static String DATASOURCE_DB_NAME = "java:comp/env/jdbc/CJPOraDB";
public static CustomerDAO getCustomerDAO()
throws DAOException {
return (CustomerDAO) createDAO(CustomerDAO.class);
}
public static EmployeeDAO getEmployeeDAO()
throws DAOException {
return (EmployeeDAO) createDAO(EmployeeDAO.class);
}
// create other DAO instances
. . .
// method to create a DAO instance. Can be optimized to
// cache the DAO Class instead of creating it everytime.
private Object createDAO(Class classObj)
throws DAOException {
// create a new DAO using classObj.newInstance() or
// obtain it from a cache and return the DAO instance
}
}
agora vem outra classe, usando o padrão AbstractFactory:
package com.corej2eepatterns.dao; 2
// imports
// Abstract class DAO Factory
public abstract class DAOFactory {
// List of DAO types supported by the factory
public static final int CLOUDSCAPE = 1;
public static final int ORACLE = 2;
public static final int SYBASE = 3;
. . .
// There will be a method for each DAO that can be
// created. The concrete factories will have to
// implement these methods.
public abstract CustomerDAO getCustomerDAO()
throws DAOException;
public abstract EmployeeDAO getEmployeeDAO()
throws DAOException;
. . .
public static DAOFactory getDAOFactory(int whichFactory) {
switch (whichFactory) {
case CLOUDSCAPE:
return new CloudscapeDAOFactory();
case ORACLE:
return new OracleDAOFactory();
case SYBASE:
return new SybaseDAOFactory();
. . .
default:
return null;
}
}
}
Minha duvida é a seguinte, pelo exemplo que está acima para pegar uma conexão para o Oracle vou ter que fazer o seguinte:
OracleDAOFactory dao = OracleDAOFactory.getDAOFactory(OracleDAOFactory.ORACLE);
ao invés de eu fazer isso, não seria mais simples instanciar a classe direto, do que ter que criar um outra classe:
OracleDAOFactory dao = new OracleDAOFactory();
retirei esse exemplo do livro core j2ee patterns.
me ajudem, por favor!!!