Questions tagged [builder-pattern]
The builder-pattern tag has no summary.
61 questions
2
votes
1
answer
159
views
For N and NBuilder, should N be an instance variable of NBuilder, or all parameters of N be instance variables of NBuilder,finally new N() in build()?
As far as I know about builder pattern, for example, an object, Student:
public class Student{
private String name;
private int age;
//setter and getter
}
to apply builder pattern to ...
0
votes
0
answers
172
views
factory methods in JPA entity classes. anti pattern?
I started using factory methods in JPA Entity classes. But I'm not sure if it's an anti-pattern. If it is , then where do I place them instead ?
A similar sample is below
@Builder
@Entity
Class ...
5
votes
6
answers
3k
views
Is it a good idea to return a Builder from a Factory?
I would want to have a builder for creating a complex object, for example Employee.
I would want to be able to create my objects in two ways:
Case 1. Get Employee with default values
Case 2. Get ...
0
votes
1
answer
163
views
API design question: Should a builder perform expensive validation when building?
I'm working on a library of C++ wrappers/bindings, for another, C-ish, API.
The C'ish API has a "launch" function, which, among other things, takes a launch configuration structure (well, ...
0
votes
1
answer
143
views
How to create QueryBuilder to work with concurrent requests
I need assistance with the QueryBuilder that generates OData query. Following is my implementation and it approach has couple of issues
If user forgets to SetRootTable then it will cause serious ...
2
votes
2
answers
172
views
Is the process of building a model from a dictionary necessarily the builder pattern?
I am working on a tool that scrubs Do-Not-Call (DNC) phone records from a skip-traced CSV file.
I have this PhoneModel for child phone entries for each row. It consist of the following fields:
isDNC
...
4
votes
1
answer
290
views
GOF class diagram for Builder pattern appears to contradict its corresponding sequence diagram
In the Design Patterns: Elements of Reusable Object Oriented Software, the Gang of Four present the following canonical form for the Builder pattern:
In Appendix B the following is mentioned ...
3
votes
3
answers
592
views
How to efficiently build objects without default constructors
I am trying to explain and understand a way to create object which cannot have "default" instances. Typically I want classes which represent, as closely as possible, the real-life concept I ...
-1
votes
1
answer
251
views
How can I use builders for products with incompatible interfaces?
I am working on a program to automatically design heater units based on varying client specifications.
The process for creating each heater is quite involved and requires multiple optional steps ...
1
vote
1
answer
799
views
Builder design pattern when creating object between many layers
I stumbled on following problem, and I'm curious if it could be done better.
A while ago I wrote a factory class that looked something like this:
public class Foo
{
private IDbContext ...
4
votes
2
answers
953
views
Is it a code smell to have two different implementations of the builder design pattern, for the same model?
Shoutout to David Arno for teaching me about the builder design pattern via this thread!
I have since used that pattern althroughout the code base to abstract out creating models from data stores, ...
-1
votes
3
answers
331
views
Design pattern for creating and scheduling tests/exams
I have an Exam class that represents an examination/test:
public class Exam
{
public int Id { get; set; }
[Required]
[StringLength(maximumLength: 30, MinimumLength = 1]
public string ...
5
votes
2
answers
487
views
GoF Builder Pattern Applicability
The book Design Patterns: Elements of Reusable Object-Oriented Software says to use the builder pattern when
The algorithm for creating a complex object should be independent of the
parts that make ...
1
vote
2
answers
1k
views
Is it wrong to extend an inner static class in java? (Builder pattern)
I'm working on a java project for the university. The project is a card game in which you travel around a 2D map and fight against some enemies. My part consists of creating the deck and the cards.
I ...
2
votes
2
answers
1k
views
Builder Pattern: Is there any advantage of having instance variables + product reference instead of just product reference?
Consider the product class:
class Car {
private String color;
private String model_num;
//getters and setters for the above fields
}
Consider builder class 1:
class CarBuilder1 {
private ...
1
vote
1
answer
101
views
Can a GoF Builder implementation be considered valid without an explicit getProduct()?
For studying purpose, I've tried to create a simple PHP implementation of Builder GoF (not the Joshua Bloch's one) inspired on this slide.
The main goal of this example is to encapsulate the ...
0
votes
1
answer
347
views
Complex objects creation from parsing a file
I have these classes and I want to instantiate each one of them easily.
All the data to create them are in a json file and there will be a lot of different objects in the end. Each objects have a lot ...
7
votes
3
answers
7k
views
Design pattern: How to inject dependencies into a Command pattern
I am pretty new to programming languages and only have limited knowledge about design patterns, so I hope you can help me with the following problem:
I have an application that operates on a group of ...
2
votes
2
answers
3k
views
Builder Pattern: Is it acceptable to use "passing-by-reference" on Director methods?
For teaching purposes, I am trying to create a PHP implementation of a conceptual example of Builder Pattern:
First of all, some products:
class Product1 {
private string $attribute1;
...
1
vote
1
answer
315
views
Mixing Creational Patterns - Prototype and Builder
Out of 5 creational design, would Builder and Prototype mixture be a valid use?
The reason for this question is - Builder assists building complex objects with various combinations of attributes. But ...
0
votes
3
answers
4k
views
Builder Pattern - Must it be part of The same Class?
For complex object creation, I am quite fond of Builder Pattern. My 1st question is "Do I need the Builder to be inside my complex class blueprint, or Could I actually have it outside?"
e.g. Are the ...
2
votes
2
answers
2k
views
Creational design pattern that allows configuration of objects
With the factory pattern we abstract the creation of objects. But what if we need a specific configuration of an object that depends on the calling context?
Example:
So I have a Builder pattern for ...
0
votes
1
answer
673
views
how to use Builder pattern combined with protected access modifier?
I'm reading a book in Design Patterns, and below is some code example used by the author. The author tries to build a html builder as (code in C#):
public class HtmlElement
{
public string Name, ...
2
votes
2
answers
369
views
Designing a builder as a compile-time state machine
I'm working on a text GUI library which comes with builders for UI components (Buttons, Panels, etc). I have a base class for these builders which I'd like to redesign so that meaningless component ...
0
votes
0
answers
124
views
How to cope with terminal operations in a fluent API?
I am trying to polish my data-mapping library and struggle with my fluent API design as it feels clumsy in some basic use-cases.
The library focuses on defining mapping objects from one type into ...
0
votes
2
answers
785
views
Java - difference between constructor and calling the object multiple times [duplicate]
I read through a code example on github
and instead of initializing the object using a constructor, they made every setter return the object itself to call it over and over again
See, constructors
...
2
votes
4
answers
9k
views
Builder pattern: How to verify required fields before runtime
A language agnostic approach since I see this problem in both compiled and interpreted languages with the builder pattern.
Let's say I have a Model that has 10 required fields and 5 optional fields. ...
37
votes
9
answers
18k
views
Why do we need a Builder class when implementing a Builder pattern?
I have seen many implementations of the Builder pattern (mainly in Java). All of them have an entity class (let's say a Person class), and a builder class PersonBuilder. The builder "stacks" a variety ...
6
votes
1
answer
1k
views
Approach for Constructing View Models in Complex MVVM Application
I'm struggling with the design in a WPF MVVM application. In a few courses I've taken, they say that having a lot of parameters in a constructor is a code smell, but they never address how to deal ...
1
vote
1
answer
4k
views
Builder pattern in multiple stages
I like using the Builder/Director pattern to build complex objects. It works really well. The only restrictions (maybe it is not really a restrictions) I see is that all of the parameters for the ...
2
votes
2
answers
2k
views
Is it an antipattern to introduce complexity into a builder?
I've looked at various definitions of the builder pattern and whilst there's varying definitions, they tend to be focused on the broad definition of incremental construction. However, it seems that ...
2
votes
1
answer
207
views
Best methods to account for large block of variables?
The question sounds retrograde but I have about 50 variables being passed to a class. Basically an array of data I call 'filters' that is first passed to a prepFiltersMethod($filters). This prep ...
1
vote
1
answer
115
views
Using Builder Pattern to standardize Return data
Would it make sense for me to use a builder pattern to return data from an API? Currently I'm just creating the array structure and returning it. In more detail, I have 3 exit places for my class that ...
7
votes
3
answers
8k
views
Is the builder pattern applicable in domain driven design?
I asked a question on StackOverflow regarding how to 'best' use the Builder pattern for constructing a Value Object which could be constructed with or without optional parameters.
One answer stated ...
1
vote
1
answer
219
views
Separation of concerns in object creation with factory and model
Thinking about the overall architecture of the application logic:
Current status: The model contains all resources that are used at runtime.
In a factory you register a builder which is used to ...
2
votes
2
answers
9k
views
what is an empty method and how are they used?
What is an empty method and how are they used?
I was reading a documented about the BUILDER Pattern and I got a curious about how it is implemented in C++. The author defines that in C++ could be ...
2
votes
1
answer
7k
views
Builder with constructor or factory method?
Let's say I have a class Dot with a builder:
public class Dot {
private final Double x;
private final Double y;
private final Color color;
private Dot(Double x, Double y, Color color)...
3
votes
3
answers
3k
views
Design pattern to force client of a class to call a method
I have a large class with complex properties. I'd like to introduce a default implementation, allow the user to override part of the default implementation and also make sure the user calls a sync ...
5
votes
3
answers
2k
views
Is it a bad practice to use an object as a Builder's only field instead of mimicking the class fields?
I see this sometimes:
class SomeClass {
Object param1, param2, param3, param4;
private SomeClass(){}
static class Builder {
SomeClass someClassInstance = new SomeClass();
// ...
3
votes
1
answer
5k
views
How to handle "conditional fields" in Java?
I've run into several situations where a POJO where whether a field value is meaningful depends on the value of another field. An example, using Lombok (which we try to use to avoid boilerplate):
@...
7
votes
4
answers
9k
views
When should the builder design pattern be used?
I am currently learning about various object oriented design patterns. I came across a pattern called the builder pattern which is basically where you build a complex object through the use of ...
5
votes
1
answer
15k
views
Is mixing Builders and Factory pattern a good idea?
I have an object Carconstructed using builder pattern.
Issue was a lot of code redundancy.
Car.Builder builder = new Car.Builder("Mercedes");
builder.numDoors(carConfig.getNumDoors()
...
3
votes
1
answer
276
views
Is Pairing a bloated interface with an Enum a good idea?
At work we have an interface that is getting bloated. The interface is designed to be easily implemented by an immutable object. So it looks something like this:
//there is no behavior here, just ...
12
votes
3
answers
5k
views
Is there any point in using builders and fluid interfaces with object initialisers?
In Java and C#, you can create an object with properties that can be set at initialisation by either defining a constructor with parameters, defining each property after constructing the object, or ...
8
votes
3
answers
3k
views
Why is the builder-pattern often implemented like this?
Often I see the implementation of the builder pattern (in Java) to be like this:
public class Foo {
private Foo(FooBuilder builder) {
// get alle the parameters from the builder and apply ...
5
votes
1
answer
2k
views
GoF's implementation of Builder in real life
I'm trying to understand builder pattern usages and so to call to separate is usage types in groups. Here is what I discovered:
Builder can be used to provide immutability (avoiding telescoping) for ...
11
votes
4
answers
6k
views
Java: How to implement a step builder for which the order of setters doesn't matter?
Edit: I'd like to point out that this question describes a theoretical problem, and I am aware that I can use constructor arguments for mandatory parameters, or throw a runtime exception if the API is ...
14
votes
5
answers
11k
views
Is it strange for a Builder object to have getter methods?
I have a fairly complex immutable data type that I'm using a builder object to instantiate. Currently, I have a setup where I parse a file, setting various fields in my builder, and then build the ...
23
votes
5
answers
2k
views
Why would a type be coupled with its builder?
I've recently deleted a java answer of mine on Code Review, that started like this:
private Person(PersonBuilder builder) {
Stop. Red flag. A PersonBuilder would build a Person; it knows about a ...
22
votes
8
answers
5k
views
How can I promote the use of the Builder pattern in my team?
Our codebase is old and new programmers, like myself, quickly learn to do it the way it's done for the sake of uniformity. Thinking that we have to start somewhere, I took it upon myself to refactor a ...