DAO/Abstract Factory Pattern - multiple data sources

Andrew Keller picture Andrew Keller · Feb 10, 2012 · Viewed 10.8k times · Source

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?

Answer

michaelliu picture michaelliu · Feb 14, 2012

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.