Questions tagged [builder]

An object creation software design pattern, one of the Gang of Four's creational design patterns.

The Builder design pattern separates the construction of a complex object from its representation.

It has two common variations:

  • The pattern as outlined in the book "Design Patterns" by the "Gang of Four" (Gamma, Helm, et al) (an example can be found on the Wikipedia page)
  • The pattern as outlined in the book "Effective Java" by Joshua Bloch (Example)

The latter is in many ways a consolidation of the participants in the GoF pattern. For example, the director is typically the product, and there is typically no "abstract builder", only a single concrete builder.

Builder is usually very helpful in solving the problem of telescoping constructors by reducing the combinations of constructors required for optional construction-time fields.

This is one of the Gang of Four's creational , first published in Gamma et al.'s book "Design Patterns: Elements of Reusable Object-Oriented Software".

1895 questions
14
votes
3 answers

Default field value with @Builder or @Getter annotation in Lombok

I'm using Lombok @Builder annotation, but I'd like some of the String fields to be optional and default to "" to avoid NPEs. Is there an easy way to do this? I can't find anything. Alternately, a way to customize @Getter to return a default value…
Hazel T
  • 859
  • 1
  • 8
  • 22
14
votes
6 answers

Builders in Java versus C++?

In Google's Protocol Buffer API for Java, they use these nice Builders that create an object (see here): Person john = Person.newBuilder() .setId(1234) .setName("John Doe") .setEmail("jdoe@example.com") .addPhone( …
Frank
  • 64,140
  • 93
  • 237
  • 324
14
votes
3 answers

PHP Builder pattern without inner classes

I've been reading through Effective Java by Joshua Bloch. I also develop in PHP and I wanted to implement the builder pattern outlined in item 2, but PHP doesn't have inner classes. Is there any way to achieve this pattern in PHP, keeping the…
Jackson
  • 607
  • 2
  • 6
  • 20
13
votes
1 answer

creating large file xml in ruby

I want to write approximately 50MB of data to an XML file. I found Nokogiri (1.5.0) to be efficient for parsing when just reading and not writing. Nokogiri is not a good option to write to an XML file since it holds the complete XML data in memory…
Gaurav Shah
  • 5,223
  • 7
  • 43
  • 71
13
votes
2 answers

Do we need a .build() method in the Builder Pattern?

I had a question regarding the "Builder Pattern" covered in "Effective Java". Do we need a .build() method for it to correctly implement the pattern? For instance, let's say that we have the following class: public class CoffeeDrink { private…
victormejia
  • 1,174
  • 3
  • 12
  • 30
12
votes
1 answer

How to set Interface Builder document versioning default to 4.2?

When I create a new xib in interface builder the Interface Builder Document (Versioning) development parameter is defaulting to xCode 4.1 Is there anyway to default this to xCode 4.2 - The reason I ask is I recently changed a project to use IOS 5…
Craig Mellon
  • 5,399
  • 2
  • 20
  • 25
12
votes
1 answer

Additional methods in java Builder class (lombok annotation)

SO, I have class that uses @Builder lombok annotation. This is how it looks and how I use it: import lombok.Builder; import lombok.Data; import com.fasterxml.jackson.annotation.JsonProperty; @Data @Builder public class MyModel { …
Bilberryfm
  • 507
  • 2
  • 11
  • 28
12
votes
5 answers

How do I use CriteriaBuilder to write a left outer join when relationship goes the other way?

I’m using JPA 2.0, Hibernate 4.1.0.Final and MySQL 5.5.37. I have the following two entities … @Entity @Table(name = "msg") public class Message { @Id @NotNull @GeneratedValue(generator = "uuid-strategy") @Column(name = "ID") …
Dave
  • 15,639
  • 133
  • 442
  • 830
12
votes
2 answers

Static Query Building with NEST

I'm playing around with Elasticsearch and NEST. I do have some trouble understanding the various classes and interfaces which can be used to create and build static queries. Here's a simplified example of what I want to achieve: using Nest; using…
Roemer
  • 2,012
  • 1
  • 24
  • 26
12
votes
5 answers

What's the clojure way to builder pattern?

We usually use builder pattern in java, like this: UserBuilder userBuilder = new UserBuilder(); User John = userBuiler.setName("John") .setPassword("1234") .isVip(true) …
qiuxiafei
  • 5,827
  • 5
  • 30
  • 43
11
votes
6 answers

How to mark a method obligatory?

Suppose you create a class names Person using the builder pattern, and suppose the Builder class contains methods body(), head(), arms() and of course build() and you consider methods head() and build() obligatory for the user of this class. We…
uzilan
  • 2,554
  • 2
  • 31
  • 46
11
votes
2 answers

Inheritance with lombok annotation get errors

In my project, lombok is used to avoid writing getters and setters for a class. I have two classes Child extends Parent: @Value @Builder @AllArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) public class Parent { @Nonnull …
Kevin
  • 111
  • 1
  • 1
  • 3
11
votes
2 answers

Extra when using builder to generate XML

I'm trying to generate KML using Builder. I know their are some options out there to help with this but I will be doing some 2.2 specific things that aren't supported by the KML gems I've looked at and would generally like to be able to accomplish…
Nick
  • 8,483
  • 10
  • 46
  • 65
11
votes
5 answers

nBuilder alternative for Java

Is there any tool like nBuilder for java? I want to build objects for unit testing and I dont want to have one builder class for each entity in my domain. I am currently using mockito but it doesnt replace the functionality of nBuilder.
Pomber
  • 1,165
  • 2
  • 9
  • 13
11
votes
2 answers

lombok @Builder vs constructor

Could someone explain me the advantages of using lombok @Builder to create an object instead of constructor call? MyObject o1 = MyObject.builder() .id(1) .name("name") .build(); MyObject o2 = new…
Vitalii
  • 10,091
  • 18
  • 83
  • 151