我一直想知道是否有办法替换当前的switch语句。下面是我拥有的代码的示例,尽管我拥有的语句更长,并且只会变得更大。通过文件读取器调用switch方法,因此它将读取一行,然后使用指定的值调用此函数。
public static void example(String action, String from, String to){
switch (action) {
case ("run"):
runTo(from,to);
break;
case ("walk"):
walkTo(from,to);
break;
case ("hide"):
hideAt(to);
break;
}
}
编辑: 我很好奇是否有更好的方法来代替上面的方案那样使用switch语句。
我对示例进行了一些更新,使其更具意义。某些方法调用不需要使用所有参数。
答案 0 :(得分:2)
对于Java 7及更低版本,我们可以声明一个函数实现接口。
对于Java 8+,我们可以使用 Function 界面。
接口:
public interface FunctionExecutor {
public Object execute(String from,String to);
}
功能上下文:
public class FunctionContect {
HashMap<String, FunctionExecutor> context=new HashMap<String, FunctionExecutor>();
public void register(String name,FunctionExecutor function){
context.put(name, function);
}
public Object call(String name,String from,String to){
return context.get(name).execute(from, to);
}
public FunctionExecutor get(String name){
return context.get(name);
}
}
函数实现:
public class RunFunctionImpl implements FunctionExecutor{
@Override
public Object execute(String from, String to) {
System.out.println("function run");
return null;
}
}
// OTHER FUCNTIONS
注册功能:
FunctionContect contex = new FunctionContect();
contex.register("run", new RunFunctionImpl());
contex.register("walk", new WalkFunctionImpl());
contex.register("hide", new HideFunctionImpl());
通话功能
context.call(action, from, to);
或
context.get(action).execute(from,to);
答案 1 :(得分:0)
我不确定您要实现什么。 如果您不想继续添加新的
case ("ccc"):
Lmn(b,c,i);
break;
块。
您可以对HashMap<string, method>
中的方法进行哈希处理,然后使用键从映射中获取方法并执行。
答案 2 :(得分:0)
如果您在同一变量上具有重复例,请在方法f
,g
和h
中说。然后,您可以将所有内容都翻过来:
void f(String a) {
switch (a) {
case "aaa": ... ; break;
...
}
}
void g(String a) {
switch (a) {
case "aaa": ... ; break;
case "bbb": ... ; break;
case "ccc": ... ; break;
...
}
}
void h(String a) {
switch (a) {
case "aaa": ... ; break;
...
}
}
可以按以下方式面向对象进行处理:
class C {
public f() { }
public g() { }
public h() { }
}
class Aaa extends C {
@Override
public f() { test3(b,c); } // Or even just the body of test3
@Override
public g() { }
@Override
public h() { }
}
class Bbb extends C {}
class Ccc extends C {}
然后必须提供特定的C:
C c;
switch (a) {
case "aaa": c = new Aaa(); break;
case "bbb": c = new Bbb(); break;
case "ccc": c = new Ccc(); break;
...
}
c.f(...);
c.g(...);
c.h(...);
这看起来是偶然的,但实际上可以改善开发质量。 添加新案例并不意味着搜索所有切换案例。
一种情况(“ aaa”)的代码全部归为一类,具有自己的专用字段。 这样可以简化事情并提供更好的概述。
答案 3 :(得分:0)
摆脱切换的一种可能选择是使用函数的哈希图:
private String stringMethod(final String action, final String source) {
final Function<String, String> toLowerFunction = String::toLowerCase;
final Function<String, String> toUpperFunction = String::toUpperCase;
final HashMap<String, Function<String, String>> stringFunctions = new HashMap<>();
stringFunctions.put("toLower", toLowerFunction);
stringFunctions.put("toUpper", toUpperFunction);
return stringFunctions.get(action).apply(source);
}
答案 4 :(得分:-1)
用方法调用替换开关绝对不是毫无意义的@Stultuske。通常,您使用方法继承,因此具有相同父类的不同子类将覆盖通用方法,而无需检查子类的类型。
另一方面,您的案例看起来像是工厂方法,但参数有点混杂。我建议将Map
与String
一起用于包装构造函数。对于“ ccc”情况,您必须考虑其他事项(例如,默认参数),或者始终使用未使用的参数i
。