如何删除大的 if-else-if 链
可能的重复:
Java 中 if 语句的长列表
我的任务是处理一些代码,并且有一个巨大的 if-else-if 链(100 多个 else-if)来检查字符串。
有哪些好的技术可以更新此代码,以便将 if-else-if 链缩小到更易于管理的位置。
链条看起来像这样:
if(name.equals("abc")){
do something
} else if(name.equals("xyz")){
do something different
} else if(name.equals("mno")){
do something different
} ......
.....
else{
error
}
Possible Duplicate:
Long list of if statements in Java
I was tasked to work with some code, and there is a giant if-else-if chain (100+ else-ifs) that checks Strings.
What are some good techniques to update this code as to where the if-else-if chain can be shrunken down to something much more manageable.
The chain looks something like this:
if(name.equals("abc")){
do something
} else if(name.equals("xyz")){
do something different
} else if(name.equals("mno")){
do something different
} ......
.....
else{
error
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以将每个分支中的代码提取到一个单独的方法中,然后将这些方法转换为公共基本接口的实现(我们称之为
Handler
)。之后,您可以填充Map
并查找并执行给定字符串的正确处理程序。不幸的是,接口的 100 多个子类的实现需要相当多的样板代码,但目前 Java 中没有更简单的方法来实现这一点。将案例实现为
Enum
的元素可能会有所帮助 - 此处是一个例子。理想的解决方案是使用闭包/lambda,但遗憾的是我们必须等到 Java 8 才能实现这一点......You can extract the code in each branch to a separate method, then turn the methods into implementations of a common base interface (let's call it
Handler
). After that, you can fill aMap<String, Handler>
and just look up and execute the right handler for given string.Unfortunately the implementation of 100+ subclasses for the interface requires quite a lot of boilerplate code, but currently there is no simpler way in Java to achieve this. Implementing the cases as elements of an
Enum
may help somewhat - here is an example. The ideal solution would be using closures / lambdas, but alas we have to wait till Java 8 for that...一些选项/想法:
hashMap.get(name).doSomething();
namedObject.doSomething()
Some options / ideas:
hashMap.get(name).doSomething();
namedObject.doSomething()
就像马特·鲍尔在他的评论中所说的那样,您可以使用命令模式。定义 Runnable 类的集合:
然后您可以使用从键到 runnables 的映射:
最后,将 if-else 链替换为:
Like Matt Ball said in his comment, you can use a command pattern. Define a collection of Runnable classes:
Then you can use a map from your keys to runnables:
Finally, replace the if-else chain with:
使用枚举,您可以为每个实例拥有一个方法。
它仍然有点混乱(有 100 多个条目的大枚举,即使它所做的只是调度),但可以避免 HashMap 初始化代码(在我看来 100 多个 put 也很混乱)。
还有另一种选择(出于文档目的)是反射:
每种方法都有其权衡:
With Enums, you can have a method per instance.
It is still kinda messy (big enum with 100+ entries, even it all it does is dispatching), but may avoid the HashMap initialization code (100+ puts is also messy in my opinion).
And yet another option (for documentation purposes) would be reflection:
Each approach has it's trade offs:
您可以使用 switch 语句,但带有字符串大小写的 Switch 语句已在 Java SE 7 中实现
最好的解决方案是使用命令模式
you can use the switch statement , but Switch statements with String cases have been implemented in Java SE 7
the best solution is to use the command pattern
这是一种流行的 Arrow 反模式,Jeff 在他的文章中讨论了一些很好地处理这个问题的方法此处。
This is a popular Arrow Anti-Pattern and Jeff discusses some approaches to handle this very nicely in his post here.