If the caller decides which logic runs by passing different lines, then why not just call their different methods:
public void doSomething(String actionID) {...}
...
doSomething("dance");
doSomething("sleep");
VS :.
public void dance() {...}
public void sleep() {...}
...
dance();
sleep();
It seems that you are unnecessarily redirecting all calls to doSomething
But strings may not always be literals. What if you take them from the console?
You can provide static mappings from strings to the corresponding functions:
class MyClass {
private static final Map<String, Consumer<MyClass>> map = new HashMap<>();
static {
map.put("sleep", MyClass::sleep);
map.put("dance", MyClass::dance);
}
public void doSomething(String actionID) {
map.getOrDefault(actionID, MyClass::doNothing).accept(this);
}
public void dance() {
System.out.print("I'm dancing");
}
public void sleep() {
System.out.print("I'm sleeping");
}
private void doNothing() {
System.out.println("I've no idea what I'm doing");
}
}
, .