Questions tagged [builder-pattern]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1
vote
2answers
70 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
1answer
64 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, ...
3
votes
2answers
190 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
0answers
58 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
2answers
162 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 ...
1
vote
3answers
673 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. ...
30
votes
9answers
8k 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 ...
5
votes
1answer
398 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 ...
0
votes
1answer
334 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
2answers
416 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
1answer
182 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
1answer
81 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 ...
2
votes
2answers
1k 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
1answer
91 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 ...
1
vote
2answers
990 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
1answer
2k 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)...
2
votes
2answers
1k 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 ...
4
votes
3answers
676 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
1answer
2k 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): @...
3
votes
1answer
6k 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
1answer
214 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 ...
9
votes
3answers
2k 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
3answers
1k 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 ...
1
vote
1answer
1k 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 ...
10
votes
4answers
3k 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 ...
7
votes
5answers
4k 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 ...
20
votes
5answers
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 ...
22
votes
8answers
4k 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 ...
2
votes
1answer
701 views

Am I using the factory method design pattern correctly, or which creational pattern should I use?

I've been studying creational design patterns for the past week or so because I have a common use case that keeps coming up, and I can't figure out which pattern fits the bill. Here is a simplified ...
19
votes
3answers
12k views

Constructor with tons of parameters vs builder pattern

It is well know that if your class have a constructor with many parameters, say more than 4, then it is most probably a code smell. You need to reconsider if the class satisfies SRP. But what if we ...
3
votes
3answers
1k views

Pattern to use (if any) to co-ordinate loosely coupled classes with strong interdependencies

I have a collection of cooperative classes whose behaviors are interdependent upon one another. But I wish to keep them loosely coupled, so I've created appropriate interfaces. I want to determine an ...
2
votes
1answer
4k views

Intelligent builder pattern - different parameters depending on type - generics?

Lets say we have the famous Joshua Bloch Nutrition Builder and we want to change it so it be a bit like dynamic builder which restricts visibility of setters and propably uses generics : public ...
4
votes
1answer
385 views

Builder Pattern : Seperation of construction from representation

I am reading GoF, and the intent of builder is mentioned as to separate creation of complex object from its representation. I couldn't understand what representation means in this context. What does ...
31
votes
1answer
5k views

Is “StringBuilder” an application of the Builder Design Pattern?

Is the "Builder" pattern restricted to addressing the "telescoping constructor" anti-pattern, or can it be said to also address the more general problem of complicated creation of immutable objects? ...
7
votes
3answers
3k views

How to deal with constructors in large data classes [duplicate]

Several times now I have come across the situations where you have some kind of settings class that simply contains a mass of data. Often these classes are simply not valid without at least most of ...
1
vote
1answer
126 views

How/should I introduce a new pattern into my codebase [closed]

I have recently discovered the Builder pattern and the Step Builder pattern which are essentially just like object initialisers but with the Step Builder pattern you can add mandatory fields. We have ...
8
votes
1answer
374 views

How should I handle incompatible configurations with the Builder pattern?

This is motivated by this answer to a separate question. The builder pattern is used to simplify complex initialization, especially with optional initialization parameters). But I don't know how to ...
20
votes
4answers
2k views

Do named arguments replace the builder pattern?

When using a language that supports named and optional arguments, does the builder pattern no longer have a practical use? Builder: new Builder(requiredA, requiredB).setOptionalA("optional").Build()...
4
votes
1answer
925 views

When do I reuse or create a new Builder

I've implemented a Builder Pattern in a project I'm working on. I'm using this design pattern to created Fields and Components (div, tables, panel (boostrap)). So I have a Director Component class. ...