Pattern Builder一次创建多个对象(列表)



我想创建一个Builder模式来一次创建多个对象。因为在基本的构建器模式下,它只能构建一个对象。

我所做的例子:

package builders;
import java.util.function.Function;
public class UserBuilder {
private List<User> users;
private int userCount;
private UserBuilder() {
}
private UserBuilder(int userCount) {
this.userCount = userCount;
}
private UserBuilder(UserBuilder copy) {
this.users = copy.users;
this.userCount = copy.userCount;
}
public static UserBuilder user() {
return new UserBuilder();
}
public static UserBuilder users(int userCount) {
return new UserBuilder(userCount);
}
public UserBuilder withFirstname(String firstName) {
return withFirstname((unused) -> firstName);
}
public UserBuilder withFirstname(Function<Integer, String> func) {
for (int i = 0; i < users.size(); i++)
users.get(i).setFirstName(func.apply(i));
return this;
}
public UserBuilder withLastName(String lastName) {
return withLastName((unused) -> lastName);
}
public UserBuilder withLastName(Function<Integer, String> func) {
for (int i = 0; i < users.size(); i++)
users.get(i).setLastName(func.apply(i));
return this;
}
public User build() {
return user.get(0);
}
public User build(int index) {
return user.get(index);
}
public List<User> buildAll() {
return users;
}
}

用法:

// Our builder can be use to create single object like basic builder:
User user = UserBuilder.user()
.withFirstName("toto")
.withLastName("tata")
.build();
// OR it can also build 10 users and set parameters thanks to lambdas
List<User> users = UserBuilder.users(10)
.withFirstName((index) -> "toto" + index)
.withLastName("tata")
.buildAll();
// users will have firstName=toto$index and all will have lastName=tata

你觉得这个图案怎么样?
这就是我想要的,但我必须复制我所有的方法(与它的lambda版本..)
也许有一种方法可以通过添加注释或类似的东西来删除50%的方法?

有更简洁的版本吗?

更常见的方法(也在例如JavaFX中使用)是保留单个对象构建器,但重用公共字段。
在您的示例中,通过在Builder中分配列表,您并没有赢得太多。

public class User {
private String firstName;
private String lastName;
private User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "User{" +
"firstName='" + firstName + ''' +
", lastName='" + lastName + ''' +
'}';
}
public static class UserBuilder {
private String firstName;
private String lastName;
public UserBuilder firstName(String firstName) {
this.firstName = firstName;
return this;
}
public UserBuilder lastName(String lastName) {
this.lastName = lastName;
return this;
}
public User build() {
return new User(firstName, lastName);
}
}
}
User user = new User.UserBuilder().firstName("toto").lastName("tata").build();
User.UserBuilder commonBuilder = new User.UserBuilder().lastName("tata");
List<User> users = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
users.add(commonBuilder.firstName("toto" + i).build());
}
List<User> users2 = IntStream.range(0, 10).mapToObj(index -> commonBuilder.firstName("toto" + index).build()).toList();

这可能会有帮助:

在Java和Kotlin中使用构建器模式构建对象列表有多困难?

部分代码:

class ManufacturerListBuilder {

private String continent;
private final List<Manufacturer> manufacturers = new LinkedList<>();
public ManufacturerListBuilder withContinent(String continent) {
this.continent = continent;
return this;
}
ManufacturerListBuilder add(Function<ManufacturerBuilder, ManufacturerBuilder> builderFunction) {

ManufacturerBuilder builder = ManufacturerBuilder
.apply(new ManufacturerBuilder()
.withContinent(continent))
.build();
this.manufacturers.add(builder);
return this;
}
public Collection<UserFilter> getManufacturers() {
return this.manufacturers;
}
}

相关内容

  • 没有找到相关文章

最新更新