如何避免过多if语句?求重构该Java方法的可行方案
重构冗余if语句的Java方法
原方法包含大量分散的if判断,维护性差,拆分布尔方法也无法减少if数量,以下是可行的重构方案:
原方法代码
private String getFolderValue(TableRow row) { String cote = row.getCellValue("B"); String typologie = row.getCellValue("G"); String description = row.getCellValue("Q"); if (cote.startsWith("DE")) { return "Dessins"; } if (cote.startsWith("PH")){ return "Photographies"; } if(cote.startsWith("CA")) { return "Catalogues"; } if(cote.startsWith("PU") && typologie.contains("affiche")){ return "Publicité###Affiches"; } if(cote.startsWith("PU") && typologie.contains("flyer")){ return "Publicité###Flyers"; } if(cote.startsWith("PU") && description.contains("presse")){ return "Publicité###Presse"; } if(cote.startsWith("PU") && (description.contains("Facture") || description.contains("devis"))){ return "Documents###Vente"; } if(typologie.contains("Emballage")){ return "Visual Merchandising###Flyers"; } if(typologie.contains("PLV")){ return "Visual Merchandising###PLV"; } if(description.contains("Correspondances")){ return "Documents###Correspondances"; } return null; }
重构方案:规则匹配模式
核心思路是将每个判断逻辑封装为独立的规则单元,通过遍历规则列表找到匹配项,彻底消除冗余if,同时提升扩展性。
步骤1:定义规则接口
创建统一的规则接口,包含匹配逻辑和结果获取方法:
interface FolderRule { boolean matches(String cote, String typologie, String description); String getFolderPath(); }
步骤2:初始化规则列表
将原方法中的每个if判断转换为规则实例,存入列表(Java 8+可使用lambda简化定义):
private static final List<FolderRule> FOLDER_RULES = Arrays.asList( new FolderRule() { @Override public boolean matches(String c, String t, String d) { return c.startsWith("DE"); } @Override public String getFolderPath() { return "Dessins"; } }, new FolderRule() { @Override public boolean matches(String c, String t, String d) { return c.startsWith("PH"); } @Override public String getFolderPath() { return "Photographies"; } }, new FolderRule() { @Override public boolean matches(String c, String t, String d) { return c.startsWith("CA"); } @Override public String getFolderPath() { return "Catalogues"; } }, new FolderRule() { @Override public boolean matches(String c, String t, String d) { return c.startsWith("PU") && t.contains("affiche"); } @Override public String getFolderPath() { return "Publicité###Affiches"; } }, new FolderRule() { @Override public boolean matches(String c, String t, String d) { return c.startsWith("PU") && t.contains("flyer"); } @Override public String getFolderPath() { return "Publicité###Flyers"; } }, new FolderRule() { @Override public boolean matches(String c, String t, String d) { return c.startsWith("PU") && d.contains("presse"); } @Override public String getFolderPath() { return "Publicité###Presse"; } }, new FolderRule() { @Override public boolean matches(String c, String t, String d) { return c.startsWith("PU") && (d.contains("Facture") || d.contains("devis")); } @Override public String getFolderPath() { return "Documents###Vente"; } }, new FolderRule() { @Override public boolean matches(String c, String t, String d) { return t.contains("Emballage"); } @Override public String getFolderPath() { return "Visual Merchandising###Flyers"; } }, new FolderRule() { @Override public boolean matches(String c, String t, String d) { return t.contains("PLV"); } @Override public String getFolderPath() { return "Visual Merchandising###PLV"; } }, new FolderRule() { @Override public boolean matches(String c, String t, String d) { return d.contains("Correspondances"); } @Override public String getFolderPath() { return "Documents###Correspondances"; } } );
步骤3:简化原方法
遍历规则列表,返回第一个匹配的结果:
private String getFolderValue(TableRow row) { String cote = row.getCellValue("B"); String typologie = row.getCellValue("G"); String description = row.getCellValue("Q"); for (FolderRule rule : FOLDER_RULES) { if (rule.matches(cote, typologie, description)) { return rule.getFolderPath(); } } return null; }
进阶优化(Java 16+)
使用**记录类(Record)**进一步简化规则定义,减少冗余代码:
// 定义上下文记录类,封装三个参数 record RuleContext(String cote, String typologie, String description) {} // 定义通用规则记录类 record SimpleFolderRule(Predicate<RuleContext> matcher, String folderPath) implements FolderRule { @Override public boolean matches(String cote, String typologie, String description) { return matcher.test(new RuleContext(cote, typologie, description)); } @Override public String getFolderPath() { return folderPath; } } // 规则列表可简化为: private static final List<FolderRule> FOLDER_RULES = Arrays.asList( new SimpleFolderRule(ctx -> ctx.cote().startsWith("DE"), "Dessins"), new SimpleFolderRule(ctx -> ctx.cote().startsWith("PH"), "Photographies"), new SimpleFolderRule(ctx -> ctx.cote().startsWith("CA"), "Catalogues"), new SimpleFolderRule(ctx -> ctx.cote().startsWith("PU") && ctx.typologie().contains("affiche"), "Publicité###Affiches"), new SimpleFolderRule(ctx -> ctx.cote().startsWith("PU") && ctx.typologie().contains("flyer"), "Publicité###Flyers"), new SimpleFolderRule(ctx -> ctx.cote().startsWith("PU") && ctx.description().contains("presse"), "Publicité###Presse"), new SimpleFolderRule(ctx -> ctx.cote().startsWith("PU") && (ctx.description().contains("Facture") || ctx.description().contains("devis")), "Documents###Vente"), new SimpleFolderRule(ctx -> ctx.typologie().contains("Emballage"), "Visual Merchandising###Flyers"), new SimpleFolderRule(ctx -> ctx.typologie().contains("PLV"), "Visual Merchandising###PLV"), new SimpleFolderRule(ctx -> ctx.description().contains("Correspondances"), "Documents###Correspondances") );
重构优势
- 彻底消除大量if语句,代码结构更清晰
- 新增规则只需在列表中添加实例,符合开闭原则
- 每个规则逻辑独立,便于单独测试和维护
内容的提问来源于stack exchange,提问作者JhinKazama
相关产品推荐
相关产品推荐

