JavaSE选择结构——优化if-else的嵌套代码

白色玫瑰 程序猿

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

{}
1.业务场景 在业务场景中,经常会出现很复杂的if else嵌套,譬如下面这种情况: // 请按你的实际需求修改参数 public String convertCountryName(String fullName) { if ("china".equalsIgnoreCase(fullName))...

目录

1.业务场景

在业务场景中,经常会出现很复杂的if else嵌套,譬如下面这种情况:👇👇👇

// 请按你的实际需求修改参数
public String convertCountryName(String fullName) {
   if ("china".equalsIgnoreCase(fullName)) {
      return "CN";
   } else if ("america".equalsIgnoreCase(fullName)) {
      return "US";
   } else if ("japan".equalsIgnoreCase(fullName)) {
      return "JP";
   } else if ("england".equalsIgnoreCase(fullName)) {
      return "UK";
   } else if ("france".equalsIgnoreCase(fullName)) {
      return "FR";
   } else if ("germany".equalsIgnoreCase(fullName)) {
      return "DE";
   } else {
      throw new RuntimeException("unknown country");
   }
}

对于上面的代码,可能我们大体上看还挺舒服的,可读性也不错。但是假设我们的业务需要支持地球上所有国家的名字与简写的转换,那么以目前的写法,将会出现有上百个if else,这样一来,代码的可读性、可扩展性等方面都将得不到一个好的保障。

所以我们能不能考虑对这上百个if-else进行优化呢?

<hr>

2.优化代码

我在这里给出两种优化方式:①采用枚举实现;②采用Map实现。

/**
 *
 */
public enum CountryEnum {
   china("CN"),
   america("US"),
   japan("JP"),
   england("UK"),
   france("FR"),
   germany("DE"),
   ;

   private String fullName;

   CountryEnum(String fullName) {
      this.fullName = fullName;
   }

   public String getFullName() {
      return fullName;
   }
}
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class CountryNameConverter {
   // 请按你的实际需求修改参数
   public String convertCountryName(String fullName) {
      if ("china".equalsIgnoreCase(fullName)) {
         return "CN";
      } else if ("america".equalsIgnoreCase(fullName)) {
         return "US";
      } else if ("japan".equalsIgnoreCase(fullName)) {
         return "JP";
      } else if ("england".equalsIgnoreCase(fullName)) {
         return "UK";
      } else if ("france".equalsIgnoreCase(fullName)) {
         return "FR";
      } else if ("germany".equalsIgnoreCase(fullName)) {
         return "DE";
      } else {
         throw new RuntimeException("unknown country");
      }
   }

   /**
    * 优化方式一:定义相关枚举类
    */
   public String chooseCountry(CountryEnum countryEnum) {
      return countryEnum.getFullName();
   }

   public String getCountry(String fullName) {
      return chooseCountry(CountryEnum.valueOf(fullName));
   }

   /**
    * 优化方式二:将这些信息存入一个map集合中
    */
   public Map<String, String> getCountryMap() {
      Map<String, String> map = new HashMap<>();
      map.put("china", "CN");
      map.put("america", "US");
      map.put("japan", "JP");
      map.put("england", "UK");
      map.put("france", "FR");
      map.put("germany", "DE");
      map.put("other", "unknown country");
      return map;
   }

   public static void main(String[] args) {
      //方式一测试
      CountryNameConverter countryName = new CountryNameConverter();
      System.out.println(countryName.getCountry("france"));
      System.out.println(CountryEnum.china.getFullName());

      //方式二测试
      Map<String, String> countryMap = countryName.getCountryMap();
      System.out.println(countryMap.get("england"));
   }
}

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

本文章网址:https://www.sjxi.cn/detil/b4f09e67f8c74a0894e07251cae3dcdb

打赏作者

本站为非盈利网站,如果您喜欢这篇文章,欢迎支持我们继续运营!

最新评论
当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号

×

(穷逼博主)在线接单

QQ: 1164453243

邮箱: abcdsjx@126.com

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