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
24
votes
2 answers

How to populate a TableView that is defined in an fxml file that is designed in JavaFx Scene Builder

I would like to know how do I populate a TableView with data... All the examples I have seen creates a TableView with columns and everything and add it to the scene. All done in the java code itself. What I want to know: if I design my "form" in…
cp5
  • 1,087
  • 6
  • 26
  • 58
23
votes
2 answers

In Kotlin, how do I add extension methods to another class, but only visible in a certain context?

In Kotlin, I want to add extension methods to a class, for example to class Entity. But I only want to see these extensions when Entity is within a transaction, otherwise hidden. For example, if I define these classes and extensions: interface…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
23
votes
5 answers

Using async Tasks with the builder pattern

I currently use the builder pattern to construct my MVC view models. var viewModel = builder .WithCarousel(), .WithFeaturedItems(3), .Build() The problem I am coming up against is when I have to…
Colin Bacon
  • 15,436
  • 7
  • 52
  • 72
23
votes
2 answers

symfony2 form querybuilder with parameters

I want to put my entity in the function of the query builder: ->add( 'weeks', 'entity', array( 'class' => 'MV\CaravanBundle\Entity\CaravanRow', 'property' => 'line', 'query_builder' => function(EntityRepository $er ) use ( $caravan ) { …
Mitchel Verschoof
  • 1,543
  • 4
  • 20
  • 38
21
votes
5 answers

How to check if AlertDialog.builder is showing and cancelling it if its showing?

Here is my code - View layout = LayoutInflater.from(this).inflate(R.layout.dialog_loc_info, null); final Button mButton_Mobile = (Button) layout.findViewById(R.id.button); AlertDialog.Builder builder = new…
Darpan
  • 5,623
  • 3
  • 48
  • 80
21
votes
5 answers

Is this Monster Builder a good Builder / Factory pattern for abstracting long constructors mixed with setters?

This is a human interface question about combining the step builder pattern with the enhanced or wizard builder patterns into a creational DSL. It uses a fluent like interface, although it uses method chaining, not cascading. That is, the methods…
candied_orange
  • 7,036
  • 2
  • 28
  • 62
21
votes
6 answers

How to build an XML document in Java concisely?

I need to build an XML document from a Java object hierarchy. Both the Java classes and the XML format are fixed. So I can't use an XML serializer like XStream: it bases the XML format on the Java classes. Likewise, a Java XML binding technology…
Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
20
votes
3 answers

StringBuilder and Builder Pattern

I'm new in Design Patterns so I have one question about the Builder Pattern. Today I heard that Builder Pattern is different from the class StringBuilder in Java, C#. I know that main goal of the Builder Pattern is to create complex objects in few…
user551761
20
votes
3 answers

Builder design pattern with inheritance: is there a better way?

I'm creating a series of builders to clean up the syntax which creates domain classes for my mocks as part of improving our overall unit tests. My builders essentially populate a domain class (such as a Schedule) with some values determined by…
cfeduke
  • 23,100
  • 10
  • 61
  • 65
20
votes
2 answers

How can I get an empty Builder for a model?

Lets say I have a function that returns a builder. In case this builder cannot be returned, I want to return an empty builder - meaning, a builder which doesn't point to any data. One which if you do a get(), you will get empty collection. Any idea?
barakuda28
  • 2,762
  • 6
  • 23
  • 32
19
votes
4 answers

Alter column length in Schema builder?

I have two fields i need to increment the character limit on. I're read through the documentation and to my surprise i found no option for it. Is it possible to do? If not, how should i go about solving this? I could drop the column and re-create it…
qwerty
  • 5,166
  • 17
  • 56
  • 77
18
votes
3 answers

Can I use the builder pattern on a Java Enum

I'm re-writing some code, and I've decided the way to recreate the class, as there are a fixed number of sheets, I'm creating them as enums. This is a decision based on the readability of a builder patter vs a telescoping constructor. The code I'm…
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
17
votes
2 answers

Custom Java query class (DSL): Builder pattern, static imports or something else for complex queries?

I am creating a custom query class, and i am unsure about the most elegant way to code it. The goals are: Easy to use Extensibility Flexible so that complex queries can be formulated Approaches Currently i can think of two alternatives. 1.…
Alp
  • 29,274
  • 27
  • 120
  • 198
17
votes
1 answer

Slack Workflow Builder url formatting

I'm trying to format a message using Slack Workflow Builder, but it doesn't work. I prepare a text as single variable and pass it to Slack webhook. In Slack Workflow Builder I receive it and send a message just placing that single variable content…
kio21
  • 639
  • 1
  • 6
  • 23
17
votes
1 answer

How to use Lombok's toBuilder on @SuperBuilder

Currently I have these three classes: @Value @NonFinal @SuperBuilder public class Parent { // Some fields } @Value @EqualsAndHashCode(callSuper = true) @SuperBuilder(toBuilder = true) public class ChildA extends Parent { // Some…
kentsurrey
  • 450
  • 1
  • 5
  • 11