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

How to inject member variables which have setter functions defined differently?

I have a builder class defined as below in a 3rd party library: public class KeyBuilder { private A a; private B b; private C c; public KeyBuilder withA(final A a){ this.a = a; return this; } public KeyBuilder withB(final B b){ this.b…
randomcompiler
  • 309
  • 2
  • 14
0
votes
0 answers

How to partially abstract a method in Scala

This question is related to this one. I have a family of classes of type Config and all of them are built using a builder pattern. Therefore, I have also ConfigBuilder classes that form a hierarchy as well, since many implementation share the same…
alexlipa
  • 1,131
  • 2
  • 12
  • 27
0
votes
1 answer

Builder pattern working with real data (not some hardcoded data)

I am currently learning the builder pattern. All the tutorials, everything is great, but when it comes to working with real data (not hard-coded strings inside your SetFoo(), SetBar() (in the builders)), I realized that I need to pass to the builder…
john
  • 5
  • 2
0
votes
1 answer

How to overload two-colon operator in Scala?

I was wishing to implement builder pattern in Scala by overloading list operator and Nil. But apparently it didn't work. class SomeBuilder { val sb : java.lang.StringBuffer = new java.lang.StringBuffer def ::(str : java.lang.String):…
Dims
  • 47,675
  • 117
  • 331
  • 600
0
votes
0 answers

Javascript ES6 generic builder pattern for large hierarchy

I am trying to come up with a generic builder function for a large hierarchy of objects in Javascript. The goal is to implement as much functionality as possible at the top of the hierarchy. After playing around for a little while, I have ended up…
0
votes
0 answers

Java MyBatis mapper for collection

I am trying to write a MyBatis Mapper for the following java objects, and the output of the mapper expects List as return type. class Blog{ private final String name; private final String author; private final List posts; // I follow builder…
technoJ
  • 175
  • 1
  • 1
  • 16
0
votes
2 answers

Builder pattern not adding values to the object

I am attempting to follow the ES6 Builder pattern, as I am now starting to construct a more complex object. So far I have: class Opinion { constructor () { this.nuts = ''; } } class Builder { constructor () { this.opinion = new…
user860511
0
votes
2 answers

Remove the Builder Class of the Builder Pattern in Effective Java

I have recently read Effective Java and I found that the Builder Pattern (item #2) is very interesting. However, I have a question: why should we create a static builder when we could do this: // JavaBeans Pattern public class NutritionFacts…
nguyentt
  • 649
  • 6
  • 13
0
votes
2 answers

C++ policy objects and the builder pattern

I have some class Builder which builds an Object. I have plans to replace some of the Object's guts with policy objects, for example being able to set some container type Storage. Specifically, I'd like to use the Builder to set policy objects of…
glfmn
  • 15
  • 4
0
votes
1 answer

Design pattern for creating object

I'd like to create object that will be created based on several other objects. I want to use Builder pattern but I have some doubts. I can see that all examples for Builder pattern show that builder has simple methods (e.g. withName(),…
John Smith
  • 173
  • 2
  • 10
0
votes
0 answers

Get value from Builder pattern in RecyclerviewAdapter

Actually, when the JSON response is received, my colleague use the Builder Pattern(implements Parcelable) instead of getter and setter so I want to implement the RecyclerViewAdapter class for creating a listView... so can anyone help me how can I…
0
votes
1 answer

C++ Builder Pattern Based on Configuration

I found this useful gist that introduced me into Builder Pattern Design. How would one implement this code to build a "Car" object with required and optional parameters based on configurations without writing dedicated builders? Scenario 1 Build a…
ccostel
  • 117
  • 1
  • 9
0
votes
1 answer

How to clone old builder to make a new builder object?

I have a builder class which I am using in one of my project. Let's say I have metricA as builder based on below class. I need to make a new builder metricB based on metricA by cloning metricA so that metricB contains all the values which were…
user5447339
0
votes
3 answers

Subclassing abstract class in a builder pattern?

I have two types of payload coming from upstream: It's either PayloadA or PayloadB. PayloadA has lot of fields in it as compared to PayloadB but there are some common fields between PayloadA and PayloadB. Just to make example simpler, I have added…
john
  • 11,311
  • 40
  • 131
  • 251
0
votes
1 answer

Usage of abstract class in the builder pattern?

I have two types of payload coming from upstream: It's either PayloadA or PayloadB. There are some common fields between PayloadA and PayloadB so I created Payload class with those common fields and for rest I created two builder class one for each…
john
  • 11,311
  • 40
  • 131
  • 251