【问题标题】:Java alternative of bad looking if-else or switch constructions外观不佳的 if-else 或 switch 结构的 Java 替代方案
【发布时间】:2018-12-21 09:34:35
【问题描述】:

寻找现代的方式来实现字符串翻译,以取代看起来很糟糕的 if-else 或 switch 结构:

if ("UK".equals(country)) 
     name = "United Kingdom";
  if ("GE".equals(country))
     name = "Germany";
  if ("FR".equals(country))
     name = "France";
  if ("IT".equals(country))
     name = "Italy";
  [...]

switch (country) {
      case "UK": name = "United Kingdom"; break;
      case "GE": name = "Germany" break;
      case "FR": name = "France"; break;
      case "IT": name = "Italy" break;
  [...]

【问题讨论】:

  • switch 有什么问题?
  • 在switch语句中需要注意break;编译器实际上没有检查的语句。我认为直到运行时才评估它是不安全的

标签: java oop design-patterns


【解决方案1】:

您可能需要enum

public enum Country {
    UK("United Kingdom"),
    GE("Germany"), // sure this isn't DE?
    FR("France");
    // and so on
    private String countryName;
    private Country(String n) { countryName = n; }

    public String getCountryName() { return countryName; }
}

现在可以

Country c = Country.valueOf(countryString); // throws exception when unknown
name = c.getCountryName();

【讨论】:

    【解决方案2】:

    我认为最简洁的方法是继承,并在您的代码中引入一些简单的数据结构来表示此类信息。这将阻止您的开关在扩展代码时升级。让我们有一个抽象基类Country,它提供一些属性,如“名称”。名称永远不会改变,并且特定于类型。所以我们定义了一个类UK,它继承自Country。现在,您可以利用多态性并迭代所有Country 类型的国家,并通过访问(只读)属性“名称”来检索其名称,而无需任何开关或类型转换。当添加一个新的国家时,你就不必再接触这种陈述了。

    List<String> countries = new ArrayList<>();
    countries.Add(new UK());
    countries.Add(new Italy());
    
    for (Country country : countries) 
    {
      String countryName = country.getName();
    }
    

    【讨论】:

      【解决方案3】:

      您可以简单地使用 java.util.Map。
      创建静态国家变量。

      private static final String UK = "UK";
      private static final String GE = "GE";
      private static final String FR = "FR";
      private static final String IT = "IT";
      
      private static final Map<String, String> COUNTRIES;
      static {
          final Map<String, String> countries = new HashMap<>();
          countries.put(UK, "United Kingdom");
          countries.put(GE, "Germany");
          countries.put(FR, "France");
          countries.put(IT, "Italy");
          COUNTRIES = Collections.unmodifiableMap(countries);
      }
      

      然后可以使用 java.util.Map 中的“get”属性来获取国家/地区名称

      System.out.println(COUNTRIES.get(UK));
      System.out.println(COUNTRIES.get(GE));
      System.out.println(COUNTRIES.get(FR));
      System.out.println(COUNTRIES.get(IT));
      

      【讨论】:

        【解决方案4】:

        另一种可能性是使用 HashMap:

        private final Map<String, String> countries = new HashMap<String, String>() {
          {
             put("UK", "United Kingdom");
             put("GE", "Germany");
             put("FR", "France");
             put("IT", "Italy");
             [...]
          }
        };
        
        name = countries.get(county);
        

        【讨论】:

          猜你喜欢
          • 2014-05-22
          • 1970-01-01
          • 2010-10-14
          • 1970-01-01
          • 1970-01-01
          • 2017-01-19
          • 1970-01-01
          • 1970-01-01
          • 2010-11-28
          相关资源
          最近更新 更多