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

How to use generics to return child type from a parent method to support builder pattern

I am trying to support a builder pattern with inheritance, to allow chaining setters from parent and child class with no issue. For this I need the parent class to know to return back the child type to keep all methods exposed for chaining. Here is…
Alon
  • 601
  • 9
  • 19
2
votes
2 answers

Multiple entries with same key immutable map error

I have a below builder class which I am using from multithread application so I have made it thread safe. Just for simplicity, I am showing only few fields here to demonstrate the problem. public final class ClientKey { private final long userId; …
user5447339
2
votes
1 answer

Spring - Injection of beans using Builder pattern

Context An application that utilizes Spring 4.1.7. All configurations are in XML files (not using annotations) and I rather keep it that way (but I can change the ways things are done if I must). Problem I have created a new class that comes with a…
Amir Keibi
  • 1,991
  • 28
  • 45
2
votes
1 answer

Can't chain method calls for builder pattern with compiled inner class

I am creating an android application that uses an aar file that has a class that uses the builder pattern. However, I suspect this is a plain old Java issue. Everything works fine if I don't use any of the chained setter methods: ProjectConfig…
Cognitio
  • 410
  • 1
  • 4
  • 14
2
votes
2 answers

how to show the value of a variable in javadoc on a method?

I have a Builder pattern in which I have a static class like as shown below: public final class DataKey { private DataKey(Builder builder) { } public static class Builder { protected int maxCount = 3; // ..some other…
user1950349
  • 4,738
  • 19
  • 67
  • 119
2
votes
1 answer

How to copy my builder object to another object and modify few fields in it?

I have a builder class as shown below: public final class RequestKey { private final Long userid; private final String deviceid; private final String flowid; private final int clientid; private final long timeout; private…
user1950349
  • 4,738
  • 19
  • 67
  • 119
2
votes
1 answer

Builder pattern with reactive programming

I'm attempting to use reactive programming (RxGroovy) to create objects using a builder pattern, where property values come from database queries. I'm looking first of all, for how to do it, and second of all, thoughts on whether or not it is a good…
brianmearns
  • 9,581
  • 10
  • 52
  • 79
2
votes
1 answer

Unit testing a fluent interface with Mockito

I want to mock the DAO interface used in the builder pattern as shown below. However when I run the test below it passes indicating that my mock object is never called. What am I doing wrong? public class DBContent { ... public static class…
user3139545
  • 6,882
  • 13
  • 44
  • 87
2
votes
0 answers

How to use nested builder pattern in json?

I have a JSON object: [ { "name": "abc", "category": "developer", "Address": [ { "street": "xxx" }, { "street": "yyy" } ] }, ........ ] I am using builder pattern to build Address as…
skm
  • 426
  • 8
  • 15
2
votes
1 answer

Writing a test data builder with syntax

Is it possible to write a TestDataBuilder which follows a given syntax? So for example: I know how to write a basic builder for a car that's not the problem. But how can I achieve, that I can only add new windows to a door? So this is allowed: var…
xeraphim
  • 4,375
  • 9
  • 54
  • 102
2
votes
1 answer

How to build abstract classes using builder pattern?

This the abstract class I have public abstract class AbstractActionModel implements IActionModel{ public abstract void getModel(); public abstract void getAction(); } Then I want to use this abstract class like below :…
armin
  • 1,985
  • 6
  • 38
  • 52
2
votes
1 answer

Java builder pattern - abstract builder

First of all, I'm relatively new to Java, so may be what I am asking is trivial, but I could not find an answer here or in other place. For simplicity, let's assume I have the following class hierarchy: class Shape { protected Shape(double x,…
Denis Itskovich
  • 4,383
  • 3
  • 32
  • 53
2
votes
2 answers

Pseudo-Backwards Builder Pattern?

In a legacy codebase I have a very large class with far too many fields/responsibilities. Imagine this is a Pizza object. It has highly granular fields like: hasPepperoni hasSausage hasBellPeppers I know that when these three fields are true, we…
2
votes
2 answers

How to make a List of interfaces to perform a action

I know the question can be answered by saying foreach(var item in items){item.doSomething()}; but what i'm after is slightly different. Here is the interface. ManagableClass .cs public interface ManagableClass : System.IDisposable { void…
Robert Snyder
  • 2,399
  • 4
  • 33
  • 65
2
votes
3 answers

How to check for invalid inputs in the Builder pattern class?

Below is my builder class in which two fields are mandatory which are userId and clientId. public final class InputKeys { private final long userId; private final int clientId; private final long timeout; private final Preference…
user2467545