Switch语句导致重复的代码



我是一名初级开发人员,希望更好地组织我的代码。

现在,我正在处理表格编号。我检索表单编号的散列映射,根据表单编号,我需要调用不同的方法。每个方法接受相同的参数,但做一些不同的事情。

例如:


var formDetails = new inferForms.buildFormsMap
for(form in formDetails){
switch(form.formNumber){
case "A1345":
getExclusionDetails(account, state, form, businessDealing)
break
case "B254":
getExclusionDetails(account, state, form, businessDealing)
break
case "B297":
getPartnershipDetails(account, state, form, businessDealing)
break
case "C397":
getBrokerageDetails(account, state, form, businessDealing)
break
case "D972":
getBrokerageDetails(account, state, form, businessDealing)
break
case "E192":
getBrokerageDetails(account, state, form, businessDealing)
break
case "E299":
getBrokerageDetails(account, state, form, businessDealing)
break
case "F254":
getLocationDetails(account, state, form, businessDealing)
break
case "F795":
getLocationDetails(account, state, form, businessDealing)
break
case "G642":
getContractDetails(period, wcmJurisdiction, newForm, wcmBusiness, frm)
break
case "G979":
getContractDetails(period, wcmJurisdiction, newForm, wcmBusiness, frm)
break
}
}

注意事项:

-方法是由另一个开发人员构建的。他退出了,所以我承担了他的工作,并希望重构使其更好。

-起始点是表单编号的HashMap。我生成HashMap,然后循环遍历它,根据HashMap中的每个表单编号收集详细信息。

-即使我要将方法转换为对象继承结构,我仍然需要一个switch语句来知道要实例化哪个子类,不是吗?switch语句就像上面这个?

-其中一些case语句调用了完全相同的方法。有办法避免重复吗?

谢谢你的帮助。我正绞尽脑汁想办法更好地重新设计它。如果我能提供更多的细节,请告诉我。

至少有一些案例具有相同的body ->使用switch语句fall through

switch(form.formNumber){
case "A1345": // fall through
case "B254":
getExclusionDetails(account, state, form, businessDealing)
break;
case "B297":
getPartnershipDetails(account, state, form, businessDealing)
break
case "C397": // fall through
case "D972": // fall through
case "E192": // fall through
case "E299":
getBrokerageDetails(account, state, form, businessDealing)
break
case "F254": // fall through
case "F795":
getLocationDetails(account, state, form, businessDealing)
break;
case "G642": // fall through
case "G979":
getContractDetails(period, wcmJurisdiction, newForm, wcmBusiness, frm)
break;
}

参见Holger的评论:

在JDK 14及更新版本中,您可以使用允许多个标签的新语法,而不会出错。

switch(form.formNumber) { 
case "A1345", "B254" -> getExclusionDetails(account, state, form, businessDealing); 
case "B297" -> getPartnershipDetails(account, state, form, businessDealing); 
case "C397", "D972", "E192", "E299" -> getBrokerageDetails(account, state, form, businessDealing); 
case "F254", "F795" -> getLocationDetails(account, state, form, businessDealing); 
case "G642", "G979" -> getContractDetails(period, wcmJurisdiction, newForm, wcmBusiness, frm); 
}

您可以用if, else if代替switch case,因为有多个条件具有相同的结果,它将减少重复。

var formDetails = new inferForms.buildFormsMap
for(form in formDetails){
var formNumber = form.formNumber
if(formNumber.equals("A1345") || formNumber.equals("A1345")){
getExclusionDetails(account, state, form, businessDealing)
} else if (formNumber.equals("B297") || formNumber.equals("C397")) {
getPartnershipDetails(account, state, form, businessDealing)
} else if (formNumber.equals("D972") || formNumber.equals("E192")) {
getBrokerageDetails(account, state, form, businessDealing)
} else if (formNumber.equals("F254") || formNumber.equals("F795")) {
getLocationDetails(account, state, form, businessDealing)
} else if (formNumber.equals("G642") || formNumber.equals("G979")) {
getContractDetails(period, wcmJurisdiction, newForm, wcmBusiness, frm)
}
}

对我来说,你的箱子似乎是工厂模式的一个很好的候选。

首先定义一个抽象来收集不同的细节。

public interface DetailsManager {
void gatherDetails(String account, String state, String form, String businessDealing);
}

继续具体实现。

public class ExclusionDetailsManager implements DetailsManager {
@Override
public void gatherDetails(String account, String state, String form, String businessDealing) {
//do stuff
}
}
public class PartnershipDetailsManager implements DetailsManager {
@Override
public void gatherDetails(String account, String state, String form, String businessDealing) {
//do other stuff
}
}

为了避免switch和if-else语句,您可以在工厂中使用Map

public class DetailsManagerFactory {
private final Map<String, Supplier<DetailsManager>> map;
private final Supplier<DetailsManager> defaultSupplier;
public DetailsManagerFactory(Map<String, Supplier<DetailsManager>> map) {
this.map = map;
this.defaultSupplier = DefaultDetailsManager::new;
}
public DetailsManager getManager(String formNumber) {
return this.map.getOrDefault(formNumber, this.defaultSupplier).get();
}
private static final class DefaultDetailsManager implements DetailsManager {
@Override
public void gatherDetails(String account, String state, String form, String businessDealing) {
//default manager doing nothing, just making sure not to cause NPE
}
}
}

DetailsManager的创建由于被包裹在Supplier中而延迟。如果对象是重量级的——只在需要的时候创建实例,这是很有用的。如果不需要,您可以将映射的值更改为DetailsManager

public class CachingDetailsManagerSupplier implements Supplier<DetailsManager> {
private final Supplier<DetailsManager> managerSupplier;
private DetailsManager cache;
public CachingDetailsManagerSupplier(Supplier<DetailsManager> managerSupplier) {
this.managerSupplier = managerSupplier;
}
@Override
public DetailsManager get() {
if (this.cache == null) {
//init manager
this.cache = this.managerSupplier.get();
}
return this.cache;
}
}

此供应商将创建的实例缓存,但根据您的具体用例,这可能是不希望的/不需要的。

例子
//init factory where appropriate
Supplier<DetailsManager> exclusionManagerSupplier = new CachingDetailsManagerSupplier(ExclusionDetailsManager::new);
Map<String, Supplier<DetailsManager>> map = new HashMap<>();
map.put("A1345", exclusionManagerSupplier);
map.put("B254", exclusionManagerSupplier);
map.put("B297", new CachingDetailsManagerSupplier(PartnershipDetailsManager::new));
DetailsManagerFactory factory = new DetailsManagerFactory(map);
//gather details
for (Object form : formDetails) {
String formNumber = form.formNumber;
DetailsManager manager = factory.getManager(formNumber);
manager.gatherDetails(account, state, form, businessDealing);
}

相关内容

  • 没有找到相关文章

最新更新