Hi following code shows abstraction between business layer and data access layer. data access can be any like using
JDBC or Hibernate.
The DAO Factory public abstract class DAOFactory
{
public static final DAOFactory HIBERNATE =
new com.vmware.vcp.dao.hibernate.HibernateDAOFactory();
public static final DAOFactory JDBC =
new com.vmware.vcp.dao.jdbc.JDBCDAOFactory();
public static final DAOFactory DEFAULT = HIBERNATE;
public static final DAOFactory getDAOFactory()
{
return DEFAULT;
}
public abstract CustomerDAO getCustomerDAO();
}
The Hibernate DAO Factory public class HibernateDAOFactory extends DAOFactory
{
public CustomerDAO getCustomerDAO()
{
return new CustomerDAOHibernate(getCurrentSession(Constants.Hibernate.MASTER_SESSION_FACTORY_NAME));
}
}
The Customer DAO interface public interface CustomerDAO {
public Customer findCustomerForUserId(
String id, boolean lock);
}
The concrete impl of customer dao
public class CustomerDAOHibernate implements CustomerDAO
{
public Customer findCustomerForUserId(String id, boolean lock)
{
// Database access code
}
}
Business layer code DAOFactory df = DAOFactory.getDAOFactory();
CustomerDAO custDao = df.getCustomerDAO();
custDao.findCustomerForUserId(UserId, true);