HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Dispatching SOAP function calls

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
soapdispatchingfunctioncalls

Problem

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).

Solution

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 serviceMethodMap = new HashMap<>();


-
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 serviceMethodMap = new HashMap<>();


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

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


-
Finally replace the switch with:

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

Code Snippets

public interface ServiceMethod {
    public void execute();
}
Map<String, ServiceMethod> serviceMethodMap = new HashMap<>();
serviceMethodMap.put("getTemperature", new ServiceMethod() {
    public void execute() {
        getDeviceTemp();
    });
serviceMethodMap.put("getBrightness", new ServiceMethod() {
    public void execute() {
        getBrightness();
    });
final ServiceMethod serviceMethod = serviceMethodMap.get(methodName);
if(serviceMethod != null) {
    serviceMethod.execute();
}
@FunctionalInterface
public interface ServiceMethod {
    public void execute();
}

Context

StackExchange Code Review Q#27503, answer score: 18

Revisions (0)

No revisions yet.