Java代码重复重构

标签 java design-patterns refactoring

我使用 codeclimate 静态分析我的 java 代码。输出显示:在 3 个位置找到类似的代码块。考虑重构。

重构以下代码而不重复自己并且不丢失“代码可读性”的最佳方法是什么:

public String getString(String component, String key, String defaultValue) throws ConfigException {
    try {
      SingleRequestData config = clientRest.target(this.configServiceUrl).path(CONFIG_ENDPOINT).path(component)
          .path(key).path(defaultValue).request(MediaType.APPLICATION_JSON).get(new GenericType<SingleRequestData>() {
          });
      logger.log(Level.INFO, "Fetched Remote config: {0}={1} for Component: {3}",
          new Object[] { key, config.value, component });
      return config.value;
    } catch (Exception e) {
      logger.log(Level.SEVERE, "{0} : {1}", new Object[] { e.getMessage(), ERROR_MESSAGE });
      throw new ConfigException(e.getMessage() + " " + ERROR_MESSAGE, e.getCause());
    }
  }

  @Override
  public Integer getInteger(String component, String key, int defaultValue) throws ConfigException {
    try {
      String value = this.getString(component, key, String.valueOf(defaultValue));
      return Integer.parseInt(value);
    } catch (Exception e) {
      logger.log(Level.SEVERE, e.getMessage(), e);
      throw new ConfigException(e.getMessage(), e.getCause());
    }
  }

  @Override
  public Double getDouble(String component, String key, double defaultValue) throws ConfigException {
    try {
      String value = this.getString(component, key, String.valueOf(defaultValue));
      return Double.parseDouble(value);
    } catch (Exception e) {
      logger.log(Level.SEVERE, e.getMessage(), e);
      throw new ConfigException(e.getMessage(), e.getCause());
    }
  }

  @Override
  public Boolean getBoolean(String component, String key, boolean defaultValue) throws ConfigException {
    try {
      String value = this.getString(component, key, String.valueOf(defaultValue));
      return Boolean.parseBoolean(value);
    } catch (Exception e) {
      logger.log(Level.SEVERE, e.getMessage(), e);
      throw new ConfigException(e.getMessage(), e.getCause());
    }
  }

谢谢

最佳答案

定义一个通用方法,调用 getString 并使用自定义解析器(在本例中定义为函数接口(interface))将字符串解析为通用类型:

public <T> T getValue(String component, String key, T defaultValue, Function<String, T> parser) throws ConfigException {
    try {
        String value = this.getString( component, key, String.valueOf(defaultValue) );
        return parser.apply(value);
      } catch (Exception e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
        throw new ConfigException( e.getMessage(), e.getCause() );
      }
}

public Integer getInteger(String component, String key, int defaultValue) throws ConfigException {
    return getValue(component, key, defaultValue, Integer::parseInt );
}

public Double  getDouble(String component, String key, double defaultValue) throws ConfigException {
    return getValue(component, key, defaultValue, Double::parseDouble );
}

public Boolean getBoolean (String component, String key, boolean defaultValue) throws ConfigException {
    return getValue(component, key, defaultValue, Boolean::parseBoolean);
}

关于Java代码重复重构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48951484/

相关文章:

javascript - 如何将 JavaScript 代码库划分为模块?

design-patterns - 有经理设计模式这样的东西吗?

c# - 有哪些可能的设计来解决这个并发问题?

java - 是否有任何开源命令行工具来重构 java 代码?

c# - IDE0063什么时候配置?

Java 8 循环 : stuck with 2nd for loop initialize variable

java - 在 Spring 配置中初始化空数组列表?

java - 阻止操作系统将对象交换到 Java 中的虚拟内存/磁盘?

java - 在 Java 中旋转 NxN 矩阵

javascript - 过滤一系列控件并操作它们