Dispatching SOAP function calls I'm receiving a certain string value over the network and I need to call a function based on that value.

So this is more or less what I have at the moment:

String methodName = soapObj.getMethodName();

switch(methodName) {
    case "getTemperature":
        getDeviceTemp();
        break;
    case "getBrightness":
        getScreenBrightness();
        break;
    ...
}


And.. there are about 60 to 70 methods supported like that.

What's the most elegant way of handling above situation without using reflection? (The app is moderately constrained performance-wise).
I would suggest to:


Define an interface with a method that will be called inside every case, e.g.

public interface ServiceMethod {
    public void execute();
}

Create a map that will contain contain instances of ServiceMethod findable by the a String key, e.g.:

Map&lt;String, ServiceMethod&gt; serviceMethodMap = new HashMap&lt;&gt;();

Fill up the serviceMethodMap map with ServiceMethod instances (this is to avoid the switch), e.g.:

serviceMethodMap.put("getTemperature", new ServiceMethod() {
    public void execute() {
        getDeviceTemp();
    });
serviceMethodMap.put("getBrightness", new ServiceMethod() {
    public void execute() {
        getBrightness();
    });   

Finally replace the switch with:

final ServiceMethod serviceMethod = serviceMethodMap.get(methodName);
if(serviceMethod != null) {
    serviceMethod.execute();
}





Update 2/18/2015, added Java 8 support


Define an interface with a method that will be called inside every case, e.g.

@FunctionalInterface
public interface ServiceMethod {
    public void execute();
}

Create a map that will contain contain instances of ServiceMethod findable by the a String key, e.g.:

Map&lt;String, ServiceMethod&gt; serviceMethodMap = new HashMap&lt;&gt;();

Fill up the serviceMethodMap map with ServiceMethod instances (this is to avoid the switch), e.g.:

serviceMethodMap.put("getTemperature", () -&gt; getDeviceTemp());
serviceMethodMap.put("getBrightness", () -&gt; getBrightness());

Finally replace the switch with:

final ServiceMethod serviceMethod = serviceMethodMap.get(methodName);
if(serviceMethod != null) {
    serviceMethod.execute();
}