英文:
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 < 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 < 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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论