具有不同 HashMap 参数的重载方法

Overload method with different HashMap parameters

我正在使用多个参数制作此方法: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);

    }

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

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

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

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

我会这样做:

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);
    }

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

我建议使用 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);

如果您需要在途中添加额外的参数,您只需要在构建器中添加一个方法,并且可以保持现有代码不变。