Patrick Jeon
Patrick Jeon

Reputation: 1814

is this valid Factory pattern usage?

I made PageFactory class for Paging the list of data regardless which DBMS is used.

But I'm not sure that this is valid factory pattern or something's wrong.

If there's better way to to this could you tell me?

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();
    }

}

Upvotes: 1

Views: 84

Answers (2)

qw20012
qw20012

Reputation: 89

As you have extract the creation process of page into a standalone class instead key word 'new'. So yes, it is a Simple Factory pattern.

Upvotes: 1

mostafa.S
mostafa.S

Reputation: 1584

As long as MysqlPageCalculator and OraclePageCalculator implement the same interface, or the same super class, let's say PageCalculator, you are implementing Abstract Factory pattern correctly.

Upvotes: 1

Related Questions