mercredi 2 octobre 2019

override value of instance variable based on user string

I am building a java based web app with a class (being created per request) that does some computation based on configured member variables. I want to make it possible for the user to experiment by changing the value of any of these instance variables by sending it as a GET param.

For eg:

class Multiply {
    public int factor = 5;

    public doMul(int n) {
        return n * factor;
    }
}

Suppose this is my class. I create an object of this class for every request and call the doMul function with the use supplied value of n. I want to build a generic framework where the user can send a GET param to override the value of this factor for a particular request (example format being "?n=10&factor=2").

What would be a good design for this use case? On option i can think of is to read values of pre-defined params like 'factor' and then use if-else to set the instance variable. Is there something more generic to be used here?

Aucun commentaire:

Enregistrer un commentaire