1

无论使用哪个 DBMS,我都为 Paging 数据列表创建了PageFactory类。

但我不确定这是有效的工厂模式还是有问题。

如果有更好的方法,你能告诉我吗?

package com.tource.cms.common.database.paging;

import com.tource.cms.common.environment.EnvironmentVariables;

public class PageFactory {

    private static final String DEFAULT_DATABASE_TYPE = getDefaultDatabaseType();

    public static Page createPage(int totalRow, int currPage, int blockRow, int blockPage) {

        if("mysql".equals(DEFAULT_DATABASE_TYPE)) 
            return new MysqlPageCalculator(totalRow, currPage, blockRow, blockPage).getPage();
        else if("oracle".equals(DEFAULT_DATABASE_TYPE))
            return new OraclePageCalculator(totalRow, currPage, blockRow, blockPage).getPage();
        else {
            try {
                throw new UnsupportedDatabaseException();
            } catch (UnsupportedDatabaseException e) {
                e.printStackTrace();
                return null;
            }
        }

    }

    /** getting DBMS type from cached Memory */
    private static String getDefaultDatabaseType() {
        return EnvironmentVariables.get("cms.jdbc.databaseType").toLowerCase();
    }

}
4

2 回答 2

1

只要 MysqlPageCalculator 和 OraclePageCalculator 实现相同的接口,或者相同的超类,比方说 PageCalculator,你就正确地实现了抽象工厂模式。

于 2012-10-24T05:54:20.317 回答
1

由于您已将页面的创建过程提取到一个独立的类中,而不是关键字“新”。所以是的,这是一个简单工厂模式。

于 2012-10-24T06:05:07.307 回答