lundi 11 décembre 2017

Geting rid of switch-case block

How do I transform this switch-case block into a good code?

private static void agentFieldContructor(Agent agent, String nodeName, String value) {

    switch (nodeName) {

        case "Description":
            agent.setDescription(value);
            break;

        case "Model":
            agent.setModel(value);
            break;

        (... +18)

    }

}

agent is an object and I'm filling it with the value parameter according to the nodeName specified.
Each nodeName refers to a different agent attribute, but I'm receiving it like a String and I cannot change this. I've searched some Design Patterns but couldn't find anything that could help me.

Aucun commentaire:

Enregistrer un commentaire