mardi 14 novembre 2017

Store and call method from a HashMap

I have 2 files: ImplementationProvider and CaseHandler.

ImplementationProvider:

class ImplementationProvider {
    public void method1(Object[] params) {}
    public void method2(Object[] params) {}
    ...
    public void method100(Object[] params) {}
}

CaseHandler:

class CaseHandler {
    ImplementationProvider ip; // auto injected
    public void handle(String s, String param) {
        switch(s) {
            case USE_CASE_ONE: ip.method1(param); break;
            case USE_CASE_TWO: ip.method2(param); break;
            ...
        }
    }   
}

How can I refactor the CaseHandler so that the USE_CASE strings are keys in a HashMap where the values would be the methods? The main issue I'm having is the propagation of the parameters. Other answers here recommend using an interface, however I need to provide the parameter on runtime.

Aucun commentaire:

Enregistrer un commentaire