java--大量switch-case代码加入枚举类型进行优化

白色玫瑰 程序猿

时间: 2023-07-11 阅读: 1 字数:4134

{}
当项目存在大量的switch-case条件判断时,会把让整个项目看起来非常low,以下tich提出一种优化方法,可提高代码的维护性,让代码变得更加简洁,易维护。 先看原代码: String jrnDDZName = null; String ...

目录

当项目存在大量的switch-case条件判断时,会把让整个项目看起来非常low,以下tich提出一种优化方法,可提高代码的维护性,让代码变得更加简洁,易维护。

先看原代码:

            String jrnDDZName = null;
       String trsDDzName = null;
       String wkeCodName = null;
       
       switch (fileName)
       {
       case "FMCHKRULP":
          jrnDDZName = "BFMSYNRUX1";
          trsDDzName = "BFMSYNRUX2";
          wkeCodName = "BFMSYNRU";
          break;
          
       case "FMCHKRALP":
          jrnDDZName = "BFMSYNRAX1";
          trsDDzName = "BFMSYNRAX2";
          wkeCodName = "BFMSYNRA";
          break;

       case "FMBACDTAP":
          jrnDDZName = "BFMSYNBCX1";
          trsDDzName = "BFMSYNBCX2";
          wkeCodName = "BFMSYNBC";
          break;
         
       case "FMLECDTAP":
          jrnDDZName = "BFMSYNLCX1";
          trsDDzName = "BFMSYNLCX2";
          wkeCodName = "BFMSYNLC";
          break;
          
       case "FMLERDEFP":
          jrnDDZName = "BFMSYNLRX1";
          trsDDzName = "BFMSYNLRX2";
          wkeCodName = "BFMSYNLR";
          break;
          
       //pgwyx 20180419
       case "FMNTFDTAP":
          jrnDDZName = "BFMSYNNFX1";
          trsDDzName = "BFMSYNNFX2";
          wkeCodName = "BFMSYNNF";
          break;
          
       case "FMCHKTMLP":
          jrnDDZName = "BFMSYNTMX1";
          trsDDzName = "BFMSYNTMX2";
          wkeCodName = "BFMSYNTM";
          break;
     
       default:
          break;
       }

使用枚举:

public enum FileName {
    FMCHKRULP("BFMSYNRUX1","BFMSYNRUX2","BFMSYNRU"),
    FMCHKRALP("BFMSYNRUX2","BFMSYNRAX2","BFMSYNRA"),
    FMBACDTAP("BFMSYNBCX1","BFMSYNBCX2","BFMSYNBC"),
    FMLECDTAP("BFMSYNLCX1","BFMSYNLCX2","BFMSYNLC"),
    FMLERDEFP("BFMSYNLRX1","BFMSYNLRX2","BFMSYNLR"),
    FMNTFDTAP("BFMSYNNFX1","BFMSYNNFX2","BFMSYNNF"),
    FMCHKTMLP("BFMSYNTMX1","BFMSYNTMX2","BFMSYNTM");
    
    private String jrnDDZName;
    private String trsDDzName;
    private String wkeCodName;

    FileName(String jrnDDZName, String trsDDzName, String wkeCodName) {
        this.jrnDDZName = jrnDDZName;
        this.trsDDzName = trsDDzName;
        this.wkeCodName = wkeCodName;
    }

    public String jrnDDZName() {
        return jrnDDZName;
    }
    
    public String trsDDzName() {
        return trsDDzName;
    }
    
    public String wkeCodName() {
        return wkeCodName;
    }
    

}

简化后代码:

        FileName valueOf = Enum.valueOf(FileName.class, fileName);
        jrnDDZName = valueOf.jrnDDZName();
        trsDDzName = valueOf.trsDDzName();
        wkeCodName = valueOf.wkeCodName();

大大简化了代码量,也看起来更加简洁,更容易维护。

原文地址:https://blog.csdn.net/weianluo/article/details/82843899?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168904449916800185828444%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168904449916800185828444&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-30-82843899-null-null.142^v88^control_2,239^v2^insert_chatgpt&utm_term=java%E4%BC%98%E5%8C%96

本文章网址:https://www.sjxi.cn/detil/4552cd2020b94be6ba962f3e15beb0b8
最新评论
当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号

×

(穷逼博主)在线接单

QQ: 1164453243

邮箱: abcdsjx@126.com

前端项目代做
前后端分离
Python 爬虫脚本
Java 后台开发
各种脚本编写
服务器搭建
个人博客搭建
Web 应用开发
Chrome 插件编写
Bug 修复