简体   繁体   中英

Passing config data to concrete implementation of a DAO in Java

I am trying to implement a Data Access Object pattern with Abstract Factory to abstract out access to multiple data sources from the client code. I referred the below link.

http://www.oracle.com/technetwork/java/dataaccessobject-138824.html

My question is:- How can I pass along configuration data (like path to a data file) from my client code into the concrete implementation of the DAO?

In the link that I pasted above, under Example 9.2, there is an example of a concrete implementation where they are using hard-coded DBURL and DRIVER. What if these two needs to be passed along from the client code shown in Example 9.6?

Add a plus parameter to DAOFactory.getDAOFactory function (for example a ConnectionParams object, where you store the connection url, user, etc.) and then pass that to your DAOFactory implementation's constructor. After that your DAOFactory can use the ConnectionParams to create the connection.

The createConnection method mustn't be static in this case.

The ConnectionParams object can be like this: `

public class ConnectionParams {

    private String url;
    private String pass;
    private String file;

  /* getters and setters */
}

`

If your DAOFactory implementation requires a file argument, it can use the file field. The others can use the url, and the pass.

The other solution is to get the required params from a common properties file, or from the System.properties.

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