Question

I currently have some DAOs set up using the abstract factory pattern. It looks something like this:

public abstract class DaoFactory
    public static GetDaoFactory()
    public abstract IPersonDao GetPersonDao()
    // etc.

the static GetDaoFactory() returns an underlying SqlDaoFactory. Until today, all Daos have worked with the same SQL database. Now, I would like to add another DAO to this factory, however the DAO will interact with an external service instead of the SQL database (Let's say this is GetCompanyDao()). I would essentially like just to add this GetCompanyDao() method to the abstract DaoFactory class so the public interface is completely decoupled from the underlying implementation (no need/way to tell if the particular dao is using SQL or the external service).

Should I simply rename the SqlDaoFactory to something more appropriate and include the GetCompanyDao() method there, so that this DAO Facotry now uses SQL for some DAOs and an external service for the other? Or is there a different way to accomplish this?

Was it helpful?

Solution

Renaming it is totally upto you. DAO pattern abstracts any type of data access not necessarily a Database access. So you can definitely go ahead with your plan.

You can use a framework like spring, instead of manually creating the patterns.

I had tried hardcoding these patterns just once.

public abstract class DAOFactory {

  // List of DAO types supported by the factory
  public static final int MYSQL = 1;
  public static final int ORACLE = 2;
  public abstract UserDAO getUserDAO() throws SQLException;
  public static DAOFactory getDAOFactory(int whichFactory) {

    switch (whichFactory) {
      case MYSQL: 
          return new MySQLDAOFactory();
      case ORACLE    :
          ......


public class MySQLDAOFactory extends DAOFactory {

    public MySQLDAOFactory() {
    }
    public static final String DRIVER= "/*driver here*/";
    public static final String DBURL="/*conn string here*/";
    /* instead of using constants you could read them from an external xml file*/

    public static Connection createConnection() {
        /*create connection object here*/
        return conn;
    }
    public UserDAO getUserDAO() throws SQLException{
        return new MySQLUserDAO();
    }

public interface UserDAO {
    public int insertUser(String fname, String lname);
    public ArrayList<User> selectUsers();
}

public class MySQLUserDAO implements UserDAO {

    Connection conn=null;
    Statement s=null;
    public MySQLUserDAO() throws SQLException{
        conn = MySQLDAOFactory.createConnection();
        s = conn.createStatement();
    }

    public int insertUser(String fname, String lname) 
    {
        //implementation
    }


    public ArrayList<User> selectUsers() 
    {
        //implementation
    }

OTHER TIPS

Have you considered Strategy Pattern. The SQL or external service access logic can be implemented as ConcreteStrategy, while user only needs to see Strategy interface.

Consider using a dependency injection container (like Spring framework to obtain references to pre configured instances of your DAOs or other kind of services. For example, in Spring you can write an XML file defining DAOs accessing an Oracle database, and other file defining DAOs accessing another database vendor: Just deploy with the appropiate version and you get your app working.

Also, 2 things:

1) while the DAO pattern intent seeks to abstract out any kind of data source (database, web service, properties file, etc) its use is usually attached with database access only. Any other data source access layer you can define it like a "service" object.

2) Just as a side comment, unless you are actually planning to deploy your app to use different data sources (now or in the foreseable future) then there is no point in introducing a factory object, plus the generic DAO interface for each DAO.

You can do it like this. Look at figure 9.8 there. So what you actually do is change GetDaoFactory method in your abstract class to take parameter representing which factory you want, SqlDaoFactory or ExternalServiceDaoFactory

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top