如何在不同长度的输入上使用构建器?

huangapple go评论129阅读模式
英文:

How to use a Builder for different length of input?

问题

使用 Builder 模式来构建并返回对象根据可用参数的数量有一种定义好的顺序来调用方法目前我在使用 if-else在 Java 8 或更高版本中是否有动态使用 builder 的替代方法

public Task createTask(String[] params){
    if(params.length < 1){
        throw new IllegalArgumentException();
    }
    else if(params.length == 1){
        return new TaskBuilder().setOne(params[0]).build();
    }
    else if(params.length == 2){
        return new TaskBuilder().setOne(params[0])
                                .setTwo(params[1]).build();
    }
    else if(params.length == 3){
        return new TaskBuilder().setOne(params[0])
                                .setTwo(params[1])
                                .setThree(params[2]).build();
    }
    else if(params.length == 4){
        return new TaskBuilder().setOne(params[0])
                                .setTwo(params[1])
                                .setThree(params[2])
                                .setFour(params[3]).build();

    }
    else if(params.length == 5){
        return new TaskBuilder().setOne(params[0])
                                .setTwo(params[1])
                                .setThree(params[2])
                                .setFour(params[3])
                                .setFive(params[4]).build();

    }
    else{
        throw new IllegalArgumentException();
    }

}
英文:

I am using a Builder(pattern) to build and return an object. There is a defined order depending on the number of available arguments on how the methods should be called. Currently I use if-else blocks. Is there a java 8 or higher alternative to use the builder dynamically?

public Task createTask(String[] params){
if(params.length &lt; 1){
throw new IllegalArgumentException();
}
else if(params.length == 1){
return new TaskBuilder().setOne(params[0]).build();
}
else if(params.length == 2){
return new TaskBuilder().setOne(params[0])
.setTwo(params[1]).build();
}
else if(params.length == 3){
return new TaskBuilder().setOne(params[0])
.setTwo(params[1])
.setThree(params[2]).build();
}
else if(params.length == 4){
return new TaskBuilder().setOne(params[0])
.setTwo(params[1])
.setThree(params[2])
.setFour(params[3]).build();
}
else if(params.length == 5){
return new TaskBuilder().setOne(params[0])
.setTwo(params[1])
.setThree(params[2])
.setFour(params[3])
.setFive(params[4]).build();
}
else{
throw new IllegalArgumentException();
}
}

答案1

得分: 3

你不需要像函数引用那样的花哨东西。你所需要做的就是将构建器调用分解:

public Task createTask(String[] params){
    if (params.length < 1 || params.length > 5) {
        throw new IllegalArgumentException();
    }

    TaskBuilder builder = new TaskBuilder();

    if (params.length >= 1) { builder = builder.setOne(params[0]); }
    if (params.length >= 2) { builder = builder.setTwo(params[1]); }
    if (params.length >= 3) { builder = builder.setThree(params[2]); }
    if (params.length >= 4) { builder = builder.setFour(params[3]); }
    if (params.length >= 5) { builder = builder.setFive(params[4]); }
    
    return builder.build();
}
英文:

You don't really need anything fancy like function references. All you have to do is break the builder calls up:

public Task createTask(String[] params){
if (params.length &lt; 1 || params.length &gt; 5) {
throw new IllegalArgumentException();
}
TaskBuilder builder = new TaskBuilder();
if (params.length &gt;= 1) { builder = builder.setOne(params[0]); }
if (params.length &gt;= 2) { builder = builder.setTwo(params[1]); }
if (params.length &gt;= 3) { builder = builder.setThree(params[2]); }
if (params.length &gt;= 4) { builder = builder.setFour(params[3]); }
if (params.length &gt;= 5) { builder = builder.setFive(params[4]); }
return builder.build();
}

huangapple
  • 本文由 发表于 2020年8月22日 02:50:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63528546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定