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
43
votes
5 answers

Spring: Using builder pattern to create a bean

I use ektorp to connect to CouchDB. The way to build an ektorp HttpClient instance is to use builder pattern: HttpClient httpClient = new StdHttpClient.Builder() .host("mychouchdbhost") …
artemb
  • 9,251
  • 9
  • 48
  • 68
43
votes
4 answers

Java generics + Builder pattern

How do I call start() below? package com.example.test; class Bar {} public class Foo { final private int count; final private K key; Foo(Builder b) { this.count = b.count; this.key = b.key; } public…
Jason S
  • 184,598
  • 164
  • 608
  • 970
35
votes
5 answers

Inheritance for builders in lombok

I was trying to use lombok for my project. I have a class A: @Data @Builder public class A { Integer a1; } and a class B: @Data public class B extends A { Integer b1; @Builder public B(Integer b1, Integer a1) { super(a1); …
Abhinav Aggarwal
  • 1,205
  • 2
  • 11
  • 24
34
votes
3 answers

Can I have an abstract builder class in java with method chaining without doing unsafe operations?

I'm trying to have an abstract base class for some builder classes so I can easily reuse code between the Builder implementations. I want my builders to support method chaining therefore a method has to return "this" instance of the most specific…
Patrick Huy
  • 975
  • 1
  • 8
  • 19
34
votes
6 answers

Abstract Factory, Factory Method, Builder

It may seem as if this is question is a dupe, but please bear with me - I promise I've read the related posts (and the GOF book). After everything I've read, I still don't have it clear when to use an Abstract Factory, a Factory Method, or a…
34
votes
6 answers

How do I initialize classes with lots of fields in an elegant way?

In my application, I have to instantiate many different types of objects. Each type contains some fields and needs to be added to a containing type. How can I do this in an elegant way? My current initialization step looks something like…
Patrick
  • 12,336
  • 15
  • 73
  • 115
32
votes
4 answers

Lombok builder to check non null and not empty

I have a class with variables I don't want it to be null or empty. Is there a way to use Lombok builder to set the property? I can use @NonNull but I won't be able to verify if it is empty or not. Obviously the other option is to write my own…
user1692342
  • 5,007
  • 11
  • 69
  • 128
32
votes
6 answers

How to implement the builder pattern in Java 8?

Implement the builder pattern prior to Java 8 has lots of tedious, nearly duplicated code; the builder itself is typically boilerplate code. Some duplicate code detectors consider nearly each method of a pre-Java 8 builder as a copy of every other…
SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99
31
votes
2 answers

How to implements Lombok @Builder for Abstract class

I have classes that extend an abstract class and I don't want to put @Builder on top of the all child classes. Is there any way to implement Lombok @Builder for an abstract class?
sam
  • 1,073
  • 4
  • 13
  • 27
28
votes
1 answer

How can you use Builder Pattern for entities with JPA

I read that it's useful to use builder pattern when you have a class with a lot of parameters. I wonder how you can implement an entity using builder pattern. It would be great if you can provide sample code.
sa_vedem
  • 757
  • 1
  • 7
  • 16
28
votes
2 answers

How to use Lombok @Builder annotation on Methods

I want to have an easy way to construct test data and have found the Builder pattern to be a good fit as described here. However to reduce boilerplate codes in the component tests, even more, I have found @Builder from Project Lombok to be a nice…
user3139545
  • 6,882
  • 13
  • 44
  • 87
27
votes
6 answers

Builder pattern for polymorphic object hierarchy: possible with Java?

I have a hierarchy of interfaces, with Child implementing Parent. I would like to work with immutable objects, so I would like to design Builder classes that construct these objects conveniently. However, I have many Child interfaces, and I don't…
Riley Lark
  • 20,660
  • 15
  • 80
  • 128
27
votes
4 answers

Builder with conditional inclusion of element

I've been wondering is possible to do Builder with optional parameters more elegantly: what I have: Object with name, id, age. I have complex condition for inclusion of age and I'd like to send it to builder on success of that condition but i'd like…
Andrii Plotnikov
  • 3,022
  • 4
  • 17
  • 37
26
votes
14 answers

Rails 3 install error: "invalid value for @cert_chain"

I'm trying to install Rails 3 on a new OS X Snow Leopard machine (with dev tools installed), and when I sudo gem install rails, I get the following error: ERROR: While executing gem ... (Gem::FormatException) builder-2.1.2 has an invalid value for…
clem
  • 3,524
  • 3
  • 25
  • 41
24
votes
4 answers

Design Pattern: Builder

I have looked for a good example of a Builder pattern (in C#), but cannot find one either because I don't understand the Builder pattern or I am trying to do something that was never intended. For example, if I have an abstract automobile and…
Zachary Scott
  • 20,968
  • 35
  • 123
  • 205