简体   繁体   中英

Get a different class based on a config value in JAVA

I am working on a program that supports 3 different platforms. These platforms have identical logic, but the program would have to work with a different database for each one.

I have three different Database.java files for each platform. For example

com.myproject.dao.bmw.Database.java
com.myproject.dao.ford.Database.java
com.myproject.dao.chevy.Database.java

The Database classes all have the same method signatures. But their database connection or queries may be different.

I set the platform name, which in this case is the car make using a config.properties file. I call the methods inside the Database class depending on which platform is set in the config.properties file throughout the program many times.

I want to have to get the Database object based in what is set on the config.properties file when the program starts, while having the same object name for the database. That way each time I call the method names I would not have to use if statements or switches each time I want to use a method in the Database class.

What is the best way to achieve my goal?.

This sounds like a job for the Factory pattern.

  1. Create an interface CarDB (or ICarDb if you like the naming convention like that so you know it is an interface) that contains all the common methods

  2. Create 3 classes that implement CarDB - Ford , Bmw and Chevy

  3. Create a CarDbFactory that has a method like CarDB getDb(Params params) that given your parameters will return a CarDB - the actual one (Ford, Bmw...) depends on the paremeters.

First of all, you did not mention any reasons why you are not considering any of the existing ORM frameworks like Hibernate which is meant specifically for this job. In a nutshell, the ORM allows you to switch across the different databases easily. But if you have a strong reason for not to use the ORM framework , then you can consider the below approach.

Basically, need to define and use the DataBaseConfigFactory and set the appropriate DBConfiguration during the start up of your application as shown below:

DataBaseConfigFactory interface:

public interface DataBaseConfigFactory {
    Connection getConnection();
    void executeQuery();
}

MyProjectDataBaseConfigFactory class:

public class MyProjectDataBaseConfigFactory implements DataBaseConfigFactory {

    private static final DBConfiguration dbConfiguration;

    static {
        // Get the active db name from props file
        // Set dbConfiguration to BmwDBConfiguration or FordDBConfiguration, etc...
    }

    public Connection getConnection() {
         return dbConfiguration.getConnection();
    }

    public void executeQuery() {
        return dbConfiguration.executeQuery();
    }
}

Now define a DBConfiguration interface and all specific implementations for the operations that your bmw, ford, etc.. support

DBConfiguration class:

public interface DBConfiguration {
    //Add all methods that can be supported by DBConfiguration
}


public class BmwDBConfiguration implements DBConfiguration {
    // BMW specific implementations for DBConfiguration
}


public class FordDBConfiguration implements DBConfiguration{
    // Ford specific implementations for DBConfiguration
}

In short, you will be using DataBaseConfigFactory interface through out your application to connect with databases and if a new database is added then you need to set the DBConfiguration appropriately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM