[英]Replacing switch statement Java
我一直想知道是否有办法替换当前的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语句。
我对示例进行了一些更新,使其更具意义。 一些方法调用不需要使用所有参数。
对于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);
我不确定您要实现什么。 如果您不想继续添加新的
case ("ccc"):
Lmn(b,c,i);
break;
块。
您可以对HashMap<string, method>
的方法进行HashMap<string, method>
然后使用键从映射中获取该方法并执行它。
如果重复使用同一变量的切换条件,请在方法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”)的代码全都在一个类中,具有自己的专用字段。 这样可以简化事情并提供更好的概述。
摆脱切换的一种可能选择是使用函数的哈希图:
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);
}
用方法调用替换开关绝对不是毫无意义的@Stultuske。 通常,您使用方法继承,因此具有相同父类的不同子类将覆盖通用方法,而无需检查子类的类型。
另一方面,您的案例看起来像一个工厂方法,但是参数有些混杂。 我建议将String
Map
到包装构造函数。 对于“ ccc”情况,您必须考虑其他事项(例如,默认参数),或者始终使用未使用的参数i
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.