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
694
votes
28 answers

Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

My Activity is trying to create an AlertDialog which requires a Context as a parameter. This works as expected if I use: AlertDialog.Builder builder = new AlertDialog.Builder(this); However, I am leery of using "this" as a context due to the…
gymshoe
  • 7,495
  • 5
  • 20
  • 21
601
votes
13 answers

When would you use the Builder Pattern?

What are some common, real world examples of using the Builder Pattern? What does it buy you? Why not just use a Factory Pattern?
Charles Graham
  • 24,293
  • 14
  • 43
  • 56
174
votes
10 answers

Android AlertDialog Single Button

I'd like to have an AlertDialog builder that only has one button that says OK or Done or something, instead of the default yes and no. Can that be done with the standard AlertDialog, or would I have to use something else?
Elec0
  • 2,232
  • 3
  • 19
  • 25
149
votes
15 answers

How to get distinct values for non-key column fields in Laravel?

This might be quite easy but have no idea how to. I have a table that can have repeated values for a particular non-key column field. How do I write a SQL query using Query Builder or Eloquent that will fetch rows with distinct values for that…
gthuo
  • 2,376
  • 5
  • 23
  • 30
121
votes
12 answers

How to exclude property from Lombok builder?

I have a class called as "XYZClientWrapper" , which have following structure: @Builder XYZClientWrapper{ String name; String domain; XYZClient client; } What I want no build function generated for property XYZClient client Does Lombok…
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186
103
votes
3 answers

When should I use a FutureBuilder?

I was wondering when I should use the future builder. For example, if I want to make an http request and show the results in a list view, as soon as you open the view, should I have to use the future builder or just build a ListViewBuilder like: new…
Little Monkey
  • 5,395
  • 14
  • 45
  • 83
85
votes
8 answers

Builder Pattern and Inheritance

I have an object hierarchy that increases in complexity as the inheritance tree deepens. None of these are abstract, hence, all of their instances serve a, more or less sophisticated, purpose. As the number of parameters is quite high, I would want…
Eric Tobias
  • 3,225
  • 4
  • 32
  • 50
84
votes
8 answers

The builder pattern and a large number of mandatory parameters

To date I use the following implementation of the builder pattern (as opposed to the implementation described here): public class Widget { public static class Builder { public Builder(String name, double price) { ... } public…
speedRS
  • 1,190
  • 2
  • 10
  • 17
62
votes
2 answers

How to access attributes using Nokogiri

I have a simple task of accessing the values of some attributes. This is a simple script that uses Nokogiri::XML::Builder to create a simple XML doc. require 'nokogiri' builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml| …
Liz
  • 621
  • 1
  • 5
  • 3
61
votes
4 answers

How to open the keyboard automatically on UITextField?

I have a very simple table and when tocuh a cell it opens a new view with one UITextfield. All I want is that the keyboard will automatically opens, without the user have to touch the UITextfield. Its all done in Interface Builder, so I am not sure…
eemceebee
  • 2,656
  • 9
  • 39
  • 49
59
votes
4 answers

Conditional Builder Method Chaining Fluent Interface

I was wondering what would be the best way to implement a .When condition in a fluent interface using method chaining in a Builder object? For instance how would I implement the .WithSkill() and .When() methods in the following example: var level =…
Ken Burkhardt
  • 3,528
  • 6
  • 33
  • 45
52
votes
6 answers

Can you use a string to instantiate a class?

I'm using a Builder pattern in Python to separate a bunch of different configuration possibilities. Basically, I have a bunch of classes that are named ID... (e.g. ID12345). These all inherit from the base Builder class. In my script, I need to…
scottm
  • 27,829
  • 22
  • 107
  • 159
51
votes
5 answers

Automatically create builder for class in Eclipse

Is there a way to use an automatic builder to create builder (Joshua Bloch's Builder Pattern) for classes in Eclipse? For example an option in the menu, a plugin or something else. I could not find anything under "Refactor".
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
48
votes
7 answers

Builder design pattern: Why do we need a Director?

Recently I've come across the Builder design pattern. It seems that different authors use "Builder pattern" to refer to different flavours, so let me describe the pattern I'm asking about. We have an algorithm for creating products, i.e., objects of…
Ari
  • 3,460
  • 3
  • 24
  • 31
44
votes
4 answers

Builder Vs Decorator pattern

From When would you use the Builder Pattern?, It is said that builder pattern is appropriate for Pizza example. Why not Decorator ? by treating Cheese, Pepperoni, Bacon as additional decorations on a base pizza. Is it for the reason that…
bjskishore123
  • 6,144
  • 9
  • 44
  • 66
1
2 3
99 100