抽象工厂有两个不同的型号DB

时间:2016-03-29 10:01:23

标签: java hibernate java-ee design-patterns

我使用带有两个不同数据库模型(Oracle和DB2)的Abstract FactoryPattern,根据这种模式,我必须为每个工厂实现所有方法,但我不想拥有我的FactoryDao DB2中的Oracle方法,因为数据库模型不同。

现在的问题是,在这种情况下,最好的做法是什么,我如何实现这种模式来隔离两个模型?

FACTORY

    public abstract class DAOFactory {

        public static final int ORACLE = 1;

        public static final int DB2 = 2;

public abstract ApplicationDAO getApplicationDAO() throws HibernateException;
.
.


    public static DAOFactory getDAOFactory(int factory) {
            switch (factory) {

            case ORACLE:
                return new OracleFactoryDao();

            case DB2:
                return new Db2FactoryDao();
                }
            return null;
        }

FACTORYDAO

 public class OracleFactoryDao extends DAOFactory {

public ApplicationDAOImpl getApplicationDAO() {
        return new ApplicationDAOImpl();
    }
..}

    public class DB2FactoryDao extends DAOFactory {
public ApplicationDAOImpl getApplicationDAO() {
        return new ApplicationDAOImpl();
    }..}

1 个答案:

答案 0 :(得分:0)

你可以使用两个界面:IOracleFactoryDao和IDB2FactoryDao, 你只放置一个DB的方法。

 interface IOracleFactoryDao{
     void myOracleMethod();
 }
 interface IDB2FactoryDao{
     void myDB2Method();
 }
 public class OracleFactoryDao extends DAOFactory implements IOracleFactoryDao {
     void myOracleMethod(){...}
}
public class DB2FactoryDao extends DAOFactory implements IDB2FactoryDao{
     void myDB2Method(){...}
}