Questions tagged [builder-pattern]

Separate the construction of a complex object from its representation so that the same construction process can create different representations. This tag is a synonym of the more frequently used [builder]; please use that tag instead of this one.

This pattern allows a client object to construct a complex object by specifying only its type and content, being shielded from the details related to the object's representation. This way the construction process can be used to create different representations. The logic of this process is isolated from the actual steps used in creating the complex object, so the process can be used again to create a different object from the same set of simple objects as the first one.

Credit to OODesign Principle

This tag is a synonym of the more frequently used ; please use that tag instead of this one.

238 questions
4
votes
5 answers

Can the builder pattern ever be doing too much?

I've been studying design patterns with a study group recently, and have come to understand that the builder pattern can be very useful for creating complex objects that are made up of many (potentially optional) parts. However, is there ever a…
Nick DeFazio
  • 2,412
  • 27
  • 31
4
votes
1 answer

Too many arguments in method calls

Lately I've been torn when trying writing classes regarding the number of parameters requested. A very simple constructor example: Burger(bun, meat, cheese, lettuce) this.bun = bun this.meat = meat ... Vs Burger(grocery) this.bun =…
Neil
  • 51
  • 2
4
votes
1 answer

kotlin android - Custom Dialog with Builder Pattern and Java 8 lambda

I have a Custom Dialog class defined follow builder pattern. I have no problem with my code. But now I want to rebuild to be able to use on java 8 lambda CustomDialogList.kt class CustomDialogList(context: Context, …
Bao Tran
  • 43
  • 1
  • 4
4
votes
1 answer

Is it ok to use Strategy pattern in a Builder Pattern

I am curious to know if it is OK (Normal) to use Strategy pattern to change the behavior of a Builder object ? This is an example. Lets say we have the following classes and I want to use the builder to create certain type of view model for my web…
Raha
  • 1,959
  • 3
  • 19
  • 28
4
votes
2 answers

How to write an idiomatic build pattern with chained method calls in Rust?

Based on the following examples, its possible to write a build-pattern with chained method calls in Rust which either passes by value or by reference (with a lifetime specifier) Is it possible to create a macro to implement builder pattern…
ideasman42
  • 42,413
  • 44
  • 197
  • 320
4
votes
4 answers

Keeping builder in separate class (fluent interface)

Foo foo = Foo.builder() .setColor(red) .setName("Fred") .setSize(42) .build(); So I know there is the following "Builder" solution for creating named parameters when calling a method. Although, this only seems to work with inner…
Asperger
  • 3,064
  • 8
  • 52
  • 100
4
votes
4 answers

Passing Properties to Factory method

I have a factory method which returns implementation of an interface. The thing is - implementations have different constructor parameters. My question is - how to pass parameters through factory method to different implementations of the…
Kobe-Wan Kenobi
  • 3,694
  • 2
  • 40
  • 67
4
votes
1 answer

Improving builder pattern by doing validations at compile time

I recently started using Builder pattern in one of my projects and I am trying to add some sort of validations on my Builder class. I am assuming we cannot do this at compile time so that's why I am doing this validation at runtime. But may be I am…
john
  • 11,311
  • 40
  • 131
  • 251
4
votes
1 answer

Builder pattern multiple varargs

I'm reading up Joshua Bloch's 'Effective Java' where in Item 2, he mentions the advantages of using Builder pattern when dealing with several parameters in the constructor. All's good, until I saw the multiple var-args difference between the…
gaurav jain
  • 3,119
  • 3
  • 31
  • 48
4
votes
2 answers

Loading Java Builder Object from Yaml file

I have created a Bean Class using Builder Pattern and having issues creating an object from a yaml file. Here is a sample class (Actual class is quite big, this is just an excerpt incase if you wanted to answer with an example): public class…
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
4
votes
2 answers

Java Builder Design Pattern Redundant Field Declaration in Class and its Builder

The classic Builder Pattern requires fields to be declared in the class-to-be-built and the exact same fields to be declared in the builder class. This can lead to problems when there are many fields and, during refactoring, the field types are not…
mbmast
  • 960
  • 11
  • 25
4
votes
1 answer

How can I force eclipse to generate my setters with a return statement

I want my eclipse to return this in a generated setter. This would be very useful for the Builder-Pattern What eclipse does by default: public void set{uppercase_field_name}({field_type} {field_name}) { this.name = name; } What i want eclipse…
4
votes
4 answers

Correct usage of builder pattern

I am considering using the builder pattern in order to create complex objects within my application. However I have a concern over whether it is the correct pattern to use. Take this example; here we see several concrete PizzaBuilders being created…
user843337
4
votes
5 answers

Automatic generation of immutable class and matching builder class of a Java interface

What tools or libraries exists for Java that will take an interface only with accessor method definitions and automatically generate an immutable object class and also a "builder" class for incrementally building new instances or changing existing…
before
  • 585
  • 6
  • 12
4
votes
2 answers

Can I get an anonymous class from a builder?

I know I can create an anonymous class when manually creating an instance of it like this: ClassName c = new ClassName() { public void overridenMethod() { method body } } Some classes, however, use builder pattern to create a new instance. My…
JohnEye
  • 6,436
  • 4
  • 41
  • 67