【问题标题】:Overload method with different HashMap parameters不同HashMap参数的重载方法
【发布时间】:2021-03-03 13:34:48
【问题描述】:

我正在使用多个参数制作此方法:createExportJob (String testId, Long otherId ) 以减少重复代码。这个方法的例子应该是该方法应该使用的最小数量的参数。

private void createExportJob (String testId, Long otherId )  {
        String testname = getTestName(testId);

        Configuration config = itsExportService.getConfiguration();
        Map<String, String> params = new HashMap<>();
        params.put("EXAMPLE", config.getExample();
        params.put("EXAMPLENAME", config.getExampleName();
        params.put("TESTNAME", testName);
        ExportJob queuedJob = ExportQueue.addJob(params, testId, otherId);

    }

在某些情况下,我想在方法中包含更多 params.put 到 HashMap,如下所示:

  params.put("THEYESNO", theYesNo ? "YES" : "NO"); 

或者

  params.put("COORDINATES", String.valueOf(minX)+","+String.valueOf(minY)+"
        ',"+String.valueOf(maxX)+","+String.valueOf(maxY));

如何使用更多输入参数重载此方法? 或者有没有比重载更好的解决方案来解决这种情况?

【问题讨论】:

  • 为什么不创建一个包含所有参数的函数并检查空值
  • 这些值是否仅在作为参数发送时才适用?您希望如何从返回类型为 void 的方法返回值?
  • @AlperFıratKaya 可能,因为在所有方法调用中,您都需要传递这些空值
  • 对不起,这种方法应该是没有回报的,应该只是做出口工作。是的,这些值需要作为参数发送。
  • 您只需要两个版本,一个是带有字符串和长字符串的原始版本,另一个包含带有 additional 参数的附加 Map 参数,然后您将其添加到 params 让调用者决定它需要哪些额外的参数

标签: java function hashmap overloading


【解决方案1】:

我建议使用Builder。

class ExportJobConfigBuilder {
 Map<String, String> params = new HashMap<>();
 public Map<String, String> build() {
  return params;
 }
 // fill basic data in constructor
 public ExportJobConfigBuilder (String name, Configuration config) {
  params.put("TESTNAME", name);
  params.put("EXAMPLE", config.getExample();
  params.put("EXAMPLENAME", config.getExampleName();
  return this;
 }
 public ExportJobConfigBuilder withCoords(int minX, int minY, int maxX, int maxY) {
  params.put("COORDINATES", String.format("%d,%d,%d,%d", minX, minY, maxX, maxY);
  return this;
 }
 public ExportJobConfigBuilder withYesNo(boolean yesNo) {
  params.put("THEYESNO", theYesNo ? "YES" : "NO");
  return this;
 }
}

现在,您可以灵活地选择要添加的信息,而无需为每个组合添加单独的方法。你会打电话给它

ExportJobConfigBuilder builder = 
  new ExportJobConfigBuilder(getTestname(testId), itsExportService.getConfiguration());
builder.withCoords(minx, miny, maxx, maxy); // or not
builder.withYesNo(yesNo); // or not
Map<String, String> params = builder.build();

ExportJob job = ExportQueue.addJob(params, testId, otherId);

如果你需要在途中添加额外的参数,你只需要在builder中添加一个方法,你可以保持现有代码不变。

【讨论】:

【解决方案2】:

我会这样做:

private void createExportJob (String testId, Long otherId, Boolean theYesNo, Long minX, Long minY, Long maxX, Long maxY) {
            String testname = getTestName(testId);
    
            Configuration config = itsExportService.getConfiguration();
            Map<String, String> params = new HashMap<>();
            params.put("EXAMPLE", config.getExample();
            params.put("EXAMPLENAME", config.getExampleName();
            params.put("TESTNAME", testName);
            
            if(theYesNo != null)
                params.put("THEYESNO", theYesNo ? "YES" : "NO"); 
                
            if(minX != null) {// you can check other params also
                params.put("COORDINATES", String.valueOf(minX)+","+String.valueOf(minY)+
                            "',"+String.valueOf(maxX)+","+String.valueOf(maxY));
            }
            
            ExportJob queuedJob = ExportQueue.addJob(params, testId, otherId);
    
    }
        
    private void createExportJob (String testId, Long otherId){
        createExportJob(testId, otherId, null, null, null, null, null);
    }
    
    private void createExportJob (String testId, Long otherId, Boolean theYesNo){
        createExportJob(testId, otherId, theYesNo, null, null, null, null);
    }

如果参数太多,您可以使用另一个包含所有附加参数的映射并对其进行 null 检查。

【讨论】:

    猜你喜欢
    • 2013-03-26
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    相关资源
    最近更新 更多