builder-pattern

Java generic builder

我的梦境 提交于 2019-12-09 15:53:39
问题 Suppose I need some DerivedBuilder to extend some BaseBuilder . Base builder has some method like foo (which returns BaseBuilder ). Derived builder has method bar . Method bar should be invoked after method foo . In order to do it I can override foo method in DerivedBuilder like this: @Override public DerivedBuilder foo() { super.foo(); return this; } The problem is that BaseBuilder has a lot of methods like foo and I have to override each one of them. I don't want to do that so I tried to

Set a value at most once with the builder pattern

好久不见. 提交于 2019-12-08 18:48:23
问题 Is there a standard practice in Java, while using the builder pattern, to ensure that a member variable is set at most once. I need to make sure that the setter is called 0 or 1 times but never more. I would like to throw a RuntimeException of some type but am worried about synchronization issues as well as best-practices in this area. 回答1: There's nothing wrong with raising an exception if a user calls a method in an illegal way like you describe, but it's not terribly elegant. The idea

UnsupportedOperationException is thrown with Lombok Builder annotation

你说的曾经没有我的故事 提交于 2019-12-08 17:43:20
问题 I am using Lombok for my project. My model looks like: @Builder @Data @AllArgsConstructor public class ScreenDefinitionDTO { @Singular private List<ScreenDeclaration> screens; } I want to do next operation: String screenName = ctx.screenName().getText(); ScreenDeclaration declaration = ParsingUtils .buildScreenDeclaration(StringUtils.trim(screenName)); Where instance is created: public static ScreenDefinitionDTO buildEmptyScreenDTO() { return ScreenDefinitionDTO.builder() .screens(new

lombok - @Builder pattern in multiple shots

 ̄綄美尐妖づ 提交于 2019-12-07 08:45:49
问题 I use @Builder of lombok project, so consider I have this example: @Builder public class Client { private @Getter @Setter Integer id; private @Getter @Setter String name; } Which is equivalent to: public class Client { private @Getter @Setter Integer id; private @Getter @Setter String name; public static class Builder { private Integer id; private String name; private Builder() { } public Builder id(final Integer value) { this.id = value; return this; } public Builder name(final String value)

Generic Builder Pattern class using reflection [closed]

北战南征 提交于 2019-12-06 15:31:19
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . It would be great to make a implementation of the builder pattern with generics. In theory it would be possible to use reflection to make the following possible: MyClass myClass = GenericBuilder<MyClass>.aObject() .withThisProperty("foo") .withThatProperty(4) .build(); I already made the following

How to use builder pattern and inheritance with same attributes

余生长醉 提交于 2019-12-06 14:54:58
问题 In fact, I would like to ask if my approach is correct, as maybe I should not use builder pattern right here. I currently have the following class CsvItem : public class CsvItem { private CsvItemGroup group; private CsvItemEntity entity; private String att1; private String att2; private String att3; private String att4; private String att5; private String att6; private String att7; private String att8; CsvItem( CsvItemGroup group, CsvItemEntity entity, String att1, String att2, String att3,

lombok - @Builder pattern in multiple shots

ぃ、小莉子 提交于 2019-12-05 15:22:57
I use @Builder of lombok project , so consider I have this example: @Builder public class Client { private @Getter @Setter Integer id; private @Getter @Setter String name; } Which is equivalent to: public class Client { private @Getter @Setter Integer id; private @Getter @Setter String name; public static class Builder { private Integer id; private String name; private Builder() { } public Builder id(final Integer value) { this.id = value; return this; } public Builder name(final String value) { this.name = value; return this; } public Client build() { return new Client(this); } } public

Is this a valid Java implementation of an immutable class and the Builder pattern?

坚强是说给别人听的谎言 提交于 2019-12-05 02:51:23
The Builder implements Cloneable and overrides clone() and instead of copying every field of the builder, the immutable class keeps a private clone of the builder. This makes it easy to return a new builder and create slightly modified copies of an immutable instance. This way I can go MyImmutable i1 = new MyImmutable.Builder().foo(1).bar(2).build(); MyImmutable i2 = i1.builder().foo(3).build(); The Cloneable interface is said to be somewhat broken, but does any of this violate good java coding practice, are there any problems with this construct? final class MyImmutable { public int foo() {

Generic Builder Pattern class using reflection [closed]

筅森魡賤 提交于 2019-12-04 21:52:36
It would be great to make a implementation of the builder pattern with generics. In theory it would be possible to use reflection to make the following possible: MyClass myClass = GenericBuilder<MyClass>.aObject() .withThisProperty("foo") .withThatProperty(4) .build(); I already made the following code: public class CursistBuilder { private Cursist cursist = null; private CursistBuilder() { cursist = new Cursist("username not set", "email not set"); } public static CursistBuilder aCursist() { return new CursistBuilder(); } public CursistBuilder withNaam(String name) { cursist.setGebruikersnaam

Should i use builder pattern in DTO?

。_饼干妹妹 提交于 2019-12-04 11:44:36
问题 This might be a pretty subjetive question, but i would to know some more opinions. I've built a Rest API service with Spring MVC, and i implemented the DTO-Domain-Entity pattern. I want to know what do you think about implementing the Builder pattern in DTOs, something like public class UserResponseDTO extends AbstractResponseDTO { private String username; private Boolean enabled; public UserResponseDTO(String username, Boolean enabled) { this.username = username; this.enabled = enabled; }