jeudi 5 octobre 2017

DAO pattern - where should I put static variables?

I've a web app that use a JSP Model 2 architecture.

I use a DAO pattern too; in particular, different methods need values like productID that I want to be unique.

/**
 * Updates the productID that is unique per JVM run.
 * @return the updated value of <i>productID</i>
 * @throws ClassNotFoundException if an error occurs with the connection to the database
 */
public static synchronized int createProductID() throws ClassNotFoundException{
    int maxID = QueriesDAO.maxIDInDatabase("product");
    while(productID <= maxID) {
        productID++;
    }
    return productID++;
}

That method queries the database and gives back a new ID, that is stored in private static int productID = 0; (0 is the first value assigned).

What I want to ask is: where should I put that query and that variable?

For now, I have a package named DAO where I have all the queries, like queries for the users on UserDAO.java, or for the products on ProductDAO.java and so on. The createProductID() method and the variable linked are stored in the Main class, but that doesn't seems to me the right place. How can I change my pattern?

Using an interface can be a solution? But with the interface, I can't have private fields that can host the productID variables..

Aucun commentaire:

Enregistrer un commentaire