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
3
votes
3 answers

Java: Builder pattern vs. logical grouped objects

I read this question on how to split large constructors in java. But I am not quite sure what I shall do in my case. The question suggests that a builder pattern is the better way to go but at the same time one person in some sub sentence said "only…
Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
3
votes
6 answers

best practice/design pattern other than constructor overloading java

I have a class which has multiple constructors. Each represent different use cases. public class ABC { public ABC(int x) { ... } public ABC(ArrayList Stringarray) { ... } ..many more constructors.. } Constructor overloading…
3
votes
1 answer

Creation of Builders in Builder Pattern

I want to clarify my use of the builder pattern, in particular how the type of builder is created. In examples, it just assumes the type of builder and creates it. However, I created a CreateBuilder method in a 'ChartBuilderFactory' class which…
Seth
  • 8,213
  • 14
  • 71
  • 103
3
votes
2 answers

Returning `this` from generic parent class requires cast in child

I'm using the builder pattern, have extract repeated code into a 'helper' class but there's one aspect of repeated code I'm still not happy with. The builder pattern allows one to chain implementation code like this: Car car = new…
petemoloy
  • 695
  • 1
  • 5
  • 9
3
votes
0 answers

Builder Approach for Multiple Variations of an object

This is my first time asking a question on StackOverflow, so please bear with me. I am working on a small team, primarily focused on developing desktop applications for industrial settings using C#. I have recently started learning about design…
J. Ore
  • 31
  • 3
3
votes
1 answer

C# Code Contracts with Builder Pattern - "Possibly calling a method on a null reference"

I'm investigating Code Contracts and I'm implementing the Builder Pattern like this: public class PersonCaution { private PersonCaution() { } public string CautionType { get; private set; } public string Remarks { get; private…
Howard
  • 694
  • 5
  • 21
3
votes
2 answers

How to use builder pattern with all parameters as mandatory?

I have a builder pattern in which most likely all my parameters are going to be mandatory so I have created a long constructor as shown below in the code. public final class ResponseHolder { // all below six are related to response information …
john
  • 11,311
  • 40
  • 131
  • 251
3
votes
1 answer

Javascript builder pattern using private variables

I'm trying to create a builder pattern in Javascript that uses private variables, while providing one public accessor (fullName) that returns a mashup of all the other properties. This question and answer suggests that I can use…
brandonscript
  • 68,675
  • 32
  • 163
  • 220
3
votes
3 answers

Which design pattern can be used for the Railway ticket fare calculation?

I am developing a solution which is similar to railway ticketing. Context : 18 Railway Stations from Tamil Nadu are given. A passenger needs a ticket. For upto 5 stations journey, the fare is Rs.10. After the 5 stations, for every 5 stations, 5…
3
votes
2 answers

Android - Create a fragment using the Builder Pattern?

I have always created fragments and passed my parameters to the fragment by using the newInstance() pattern. This works great for one or two parameters. I'm creating a fragment now that has around 10 parameters, and all of them are optional. I was…
Mark
  • 1,130
  • 3
  • 17
  • 32
3
votes
1 answer

How to pass parameters for REST request to Java method?

We are building a Java SDK to simplify the access to one of our services that provide a REST API. This SDK is to be used by 3rd-party developers. One of the problems has been to find a good way to represent the optional parameters of each…
Mario Duarte
  • 3,145
  • 7
  • 27
  • 37
3
votes
2 answers

C++ Builder Pattern with Inheritance

I have a class I would like to use the builder pattern on, but it is derived from a base class whose attributes I need to access. I can't access the members of BaseClass in my implementation without making them public, even if I derive Builder from…
jake
  • 1,661
  • 1
  • 21
  • 30
3
votes
1 answer

Extending an object with Builder Pattern using an inner class

What I'm trying to do is create a class (Square) that uses a Builder pattern, and then extend this class as an inner class (MyCube) inside the Object where it's needed (DrawMyCube). For reasons that are a bit to complex to get into it's preferred…
uncrase
  • 644
  • 1
  • 8
  • 16
3
votes
2 answers

Does my builder need to be inside the class it builds?

I've just read a fantastic example of a builder pattern. In my program I'm creating a series of playerCharacter entities, which can be constructed in various ways, with some compulsory fields and some that can be added on as extra or added latter on…
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
2
votes
1 answer

Builder Pattern: what's the point of the Director?

I'm just studying the Builder pattern and do not understand the point of the Director. http://en.wikipedia.org/wiki/Builder_pattern Wouldn't just having Builder and their subclasses be sufficient? Thanks
TheWommies
  • 4,922
  • 11
  • 61
  • 79