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
2
votes
1 answer

what is wrong with this using generic implicit operator?

if I use implicit operator in non generic builder class every thing is ok: public class ReligionBuilder { private Religion _religion; public ReligionBuilder() { _religion = new Religion(){//some codes} } public…
Adrakadabra
  • 591
  • 2
  • 12
  • 23
2
votes
2 answers

Inheriting a class which is built using a static inner class Builder

I have a Class A with quite a number of member variables. In order to make it immutable and validate the member variables during construction, I made its constructor private and used an inner public static builder class (ClassABuilder) to build it.…
Abe
  • 8,623
  • 10
  • 50
  • 74
2
votes
1 answer

How can I use the builder pattern to construct various similar object types?

I am currently using the builder pattern as defined here: Previous question showing my use of the builder pattern The problem I've now encountered is a requirement to create the following structure: - ZipHolder: file metadata present * File…
speedRS
  • 1,190
  • 2
  • 10
  • 17
2
votes
3 answers

Is there an better alternative to implement Builder Pattern in Scala?

I have to create an instance of class BenchmarkOption based on the command line arguments. I certainly use pojo style, but this is not immutable. So I use Builder Pattern of Java style. Here is the class I implement : object CommandLineHelper { …
爱国者
  • 4,298
  • 9
  • 47
  • 66
2
votes
2 answers

Why do we need a 'Builder class' in Builder design pattern?

In Builder design pattern a 'Builder' inner class is used through which we set the values for the fields of that class. What is the purpose of defining this class? One of the main reasons cited for using a builder pattern is that it solves the…
Shameem94
  • 31
  • 7
2
votes
3 answers

Is there an alternative to the builder pattern that is preferred in C++?

I'm coming from Java where the builder pattern is used heavily, e.g. Foo foo = new FooBuilder() .setBar(43) .setBaz("hello, world!") .enableCache(true) .build(); Automapper for example is popular library that generates this pattern…
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
2
votes
1 answer

How to create an instance of a child class with a factory builder using generics?

I am trying to create an instance of a class using factory builder from the parent. The use case is basically a library that contains series of classes that inherit from the same class. The main goal was to reduce the amount of code in those…
TiGreX
  • 1,602
  • 3
  • 26
  • 41
2
votes
1 answer

Builder pattern without inner class

Lets say I have this builder pattern. I searched everywhere but couldn't find why would I need to use inner class if outer class has public consturctor. public class User { private final String firstName; private final String surname; private final…
iraquois
  • 37
  • 8
2
votes
1 answer

How to use a Builder for different length of input?

I am using a Builder(pattern) to build and return an object. There is a defined order depending on the number of available arguments on how the methods should be called. Currently I use if-else blocks. Is there a java 8 or higher alternative to use…
nopens
  • 721
  • 1
  • 4
  • 20
2
votes
0 answers

Trying to apply design pattern with the below methods

I have class like this as below for design project where i am using this one to generate word document. the process is we are using ReactJS UI and .Net core and when the user click on button in UI we will be sending the request to API where we can…
kumar jain
  • 93
  • 6
2
votes
1 answer

Good chain of builder pattern

i have a complex business object like so public class BusinessObject { public Team1Object object1; public Team2Object object2; public String debug; ... } I am using slight modification of chain of responsibility pattern to build…
2
votes
1 answer

Is the builder pattern suitable for the same representation/output but a slightly different process?

My API has different parameters. Depending on those, I have two slightly different processes. But output almost same. Should I use builders pattern ? For example, suppose there are different query parameters A and B: // For case A: if (A){ …
emdad
  • 85
  • 1
  • 1
  • 8
2
votes
1 answer

How to resolve the dilemma when a factory needs to create objects with multiple common parameters?

I have a set of subclasses of PageRenderer abstract class, e.g. ImagePageRenderer TextPageRenderer VideoPageRenderer. So in order to get the PageRenderer instance, a factory method is suitable. The concreate factory looks like this: public class…
Rui
  • 3,454
  • 6
  • 37
  • 70
2
votes
1 answer

strategy pattern or builder pattern?

I know the strategy pattern is a behavioral pattern that encapsulate an algorithm in a class. Also that the builder pattern is to separate the construction of a complex object. Now, I have to create different views of map (seller view, customer…
user6951396
2
votes
1 answer

Construction of command object with optional parameters in CQRS

My Command object contains a combination of required and optional information used to construct my domain object. class Command { /* | Required stuff */ private $req1; private $req2; /* | Optional stuff */ private $opt1; private…