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
0 answers

It is possible to generate Builder using google auto-value outside class?

I have class and builder for it. Builder would be used only in test code and I would like not to have it in production code. My class: public class MyClassThatIWantBuilderFor { // many fields that I want initialize using builder instead of 10 arg…
pixel
  • 24,905
  • 36
  • 149
  • 251
0
votes
1 answer

Difference between concrete builder and builder?

Builder design pattern has four principle include concrete builder and builder. What is the difference between concrete builder and builder?
Gamsh
  • 545
  • 4
  • 21
0
votes
1 answer

Builder Pattern in Ruby with YAML

I have an instance of the Builder pattern in my project right now. Currently, the supported output format is CSV, however I would now like to include YAML. Easy, I thought. I have all of the supporting code in to make the type change. I'm finding…
Mike
  • 19,267
  • 11
  • 56
  • 72
0
votes
2 answers

Builder pattern but builder in objects constructor

Am I still following the Builder pattern with the implementation below? What's confusing me is the protected constructor in the class "myClass". My first thought when it comes to the builder pattern is to hide the object that the builder is supposed…
Robert
  • 383
  • 1
  • 5
  • 20
0
votes
2 answers

Java generics with interface as type argument and using builder pattern

I have an interface Foo and an enum Bar which implements Foo. Then I'm using a simple builder pattern class Result. See code below. public interface Foo {} public enum Bar implements Foo { BAR } public class Result { public Result(T result)…
lirre8
  • 11
  • 2
0
votes
2 answers

Scala Builder pattern with phantom types

Having the below builder pattern in Scala. To simplify it, I'm using 3 instances of A such that instance1 contains only field1 and has no connection to field2 or field3. The problem is that everywhere in the code I have to use val s =…
0
votes
1 answer

How should the Builder Pattern handle undefined optionals?

I'm constructing a Builder in JavaScript, and I'm not sure how builders normally handle undefined values for optionals. I would like to think that the Builder doesn't append the optional field to the object if the field is undefined. Is that…
user3786913
0
votes
1 answer

Improve builder pattern on validation check?

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

Unable to mock while implementing builder pattern

I'm having hard time to test a class(TestClass) which uses builder pattern(BuilderClass) in logic . I'm unable to mock builder class(BuilderClass). The following is simplified version of my logic. public class TestClass { public int methodA() { …
hasanac
  • 168
  • 3
  • 13
0
votes
2 answers

What's wrong with my builder pattern?

I have a problem in realisation of Builder pattern. I have 2 classes: package course_2; import java.util.Date; public class Student { private static int idStart = 0; private final int id = idStart++; private String name; private…
Leneron
  • 25
  • 1
  • 6
0
votes
0 answers

Does a default constructor have any advantage in a class which is constructed using builder pattern using Jersey Java

I am trying to create an immutable object using Jersey Java. But I don't know if the default constructor is useful or not in a big application. Can someone guide me on this. This is my class :- package com.jersey.jaxb; import…
Raj Hassani
  • 1,577
  • 1
  • 19
  • 26
0
votes
2 answers

Generic Builder Pattern class using reflection

It would be great to make a implementation of the builder pattern with generics. In theory it would be possible to use reflection to make the following possible: MyClass myClass = GenericBuilder.aObject() …
Dave
  • 77
  • 1
  • 2
  • 10
0
votes
1 answer

Live Template for Fluent-API Builder in IntelliJ

If I wanted to generate a "fluent builder" in IntelliJ anyone know where to edit the live template that is used in the Replace Constructor with Builder feature? The above mentioned feature will automatically build a Builder pattern object though…
0
votes
1 answer

how can I return a subclass reference from superclass method

I have a superclass method which returns itself(for builder pattern). This class has several subclasses , so I want to return a reference to actual(subclass) type of the object. Something like this: class SuperClass { T…
lazyCoding
  • 481
  • 2
  • 5
  • 13
0
votes
1 answer

Builder Pattern Returning null

I am using builder pattern but after setting the feilds its returning nullpointerException: package com.test.sample1; public class MessageID { public int BOI1; public int DOI2; public int CO3; public MessageID(int BOI1,int DOI2,int CO3){ …
Charles Stevens
  • 1,568
  • 15
  • 30
1 2 3
15
16