[英]What's the best way to refactor a large switch statement in java?
I have a gross switch statement that I need to figure out how to refine. 我有一个总的切换语句,需要弄清楚如何改进。 I immediately think factory pattern, but I don't know if I want to actually use objects for these purposes. 我立即想到了工厂模式,但是我不知道是否要为这些目的实际使用对象。 How could you reduce the verbosity of this code? 您如何减少此代码的冗长性?
If I were to, I would 如果我愿意,我会
Validator
interface 定义Validator
接口 <Integer, Validator interface>
with key as index and value as the corresponding Validator
implementation 定义<Integer, Validator interface>
的映射,键为索引,值为相应的Validator
实现 Validator
implementation and run it. 在for循环中,基于索引,获取适当的Validator
实现并运行它。 HTH. HTH。
Viktor has the preferred solution but if all you are trying to do is reduce the code, but keep the structure you could. Viktor有首选的解决方案,但是如果您要做的只是减少代码,但请保留结构。
change 更改
if (isValidBorrowAmount(amount) == false) {
isValidValue = false;
print("That value does not work here");
}
to 至
isValidValue = isValidBorrowAmount(amount);
and after the switch block put 并在开关块放置后
if(!isValidValue){
print("That value does not work here");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.