Questions tagged [builder]

An object creation software design pattern, one of the Gang of Four's creational design patterns.

The Builder design pattern separates the construction of a complex object from its representation.

It has two common variations:

  • The pattern as outlined in the book "Design Patterns" by the "Gang of Four" (Gamma, Helm, et al) (an example can be found on the Wikipedia page)
  • The pattern as outlined in the book "Effective Java" by Joshua Bloch (Example)

The latter is in many ways a consolidation of the participants in the GoF pattern. For example, the director is typically the product, and there is typically no "abstract builder", only a single concrete builder.

Builder is usually very helpful in solving the problem of telescoping constructors by reducing the combinations of constructors required for optional construction-time fields.

This is one of the Gang of Four's creational , first published in Gamma et al.'s book "Design Patterns: Elements of Reusable Object-Oriented Software".

1895 questions
11
votes
4 answers

What design pattern to use for generating multiple simulations?

I have a simulation that runs at a specific height and a specific temperature: interface IGeneratable { string Name { get; } void Generate(); } interface ISimulation : IGeneratable { int Height { get; } int Temperature { get; } …
alhazen
  • 1,907
  • 3
  • 22
  • 43
11
votes
1 answer

"borrowed value does not live long enough" when using the builder pattern

I have the following code: pub struct Canvas<'a> { width: isize, height: isize, color: Color, surface: Surface, texture: Texture, renderer: &'a Renderer, } impl<'a> Canvas<'a> { pub fn new(width: isize, height: isize,…
nqe
  • 345
  • 3
  • 13
11
votes
7 answers

Is it possible to generate plain-old XML using Haml?

I've been working on a piece of software where I need to generate a custom XML file to send back to a client application. The current solutions on Ruby/Rails world for generating XML files are slow, at best. Using builder or event Nokogiri, while…
lsdr
  • 1,245
  • 1
  • 15
  • 28
11
votes
3 answers

Is there a memory and performance hit taken from using Bloch's Builder Pattern?

What is the memory and performance usage compared to creating a object with only a constructor? The usage here is in creating a Set or List that may contain million plus entries and I am concerned with the overhead of using…
WolfmanDragon
  • 7,851
  • 14
  • 49
  • 61
11
votes
3 answers

Does Class.newInstance() follow the "Abstract factory" design pattern?

I have started reading Joshua Bloch's "Effective Java" (second edition). While reading item 2 (Consider a builder when faced with many constructor parameters), there is a particular statement that the author makes w.r.t the Class.newInstance()…
divesh premdeep
  • 1,070
  • 3
  • 16
  • 28
11
votes
2 answers

Why does Ruby Builder::XmlMarkup add inspect tag to xml?

I'm trying out Builder::XMLMarkup to build some xml and it keeps adding an empty element to my xml. Why does it do this and how do I stop it? xml = Builder::XmlMarkup.new =>
doremi
  • 14,921
  • 30
  • 93
  • 148
11
votes
4 answers

Why doesn't Bloch's Builder Pattern work in C#

Consider a verbatim copy of Bloch's Builder pattern (with changes made for C#'s syntax): public class NutritionFacts { public int ServingSize { get; private set; } public int Servings { get; private set; } public int Calories { get; private…
cdmckay
  • 31,832
  • 25
  • 83
  • 114
10
votes
1 answer

set different colors for notification actions

Hi I am setting a notification for incoming call with two actions : Answer and Decline . I need to set Green color for Answer action and red for Decline . But i couldnt find a solution. Here is my code : NotificationCompat.Builder builder = new…
Liya
  • 568
  • 7
  • 28
10
votes
2 answers

Is it possible to auto complete a builder in Intellij?

For example, I have a class with a builder with 5 params, instead of me manually selecting the params and populating them, is there a way to tell Intellij to do this: MyClass myClass = MyClass.builder() .param1() …
mal
  • 3,022
  • 5
  • 32
  • 62
10
votes
3 answers

How To Convert the Builder Pattern to a Functional Implementation?

The grpc-java library is a good example of a library that utilizes the common builder pattern for creating objects with particular properties: val sslContext = ??? val nettyChannel : NettyChannel = NettyChannelBuilder .forAddress(hostIp,…
Ramón J Romero y Vigil
  • 17,373
  • 7
  • 77
  • 125
10
votes
2 answers

wither vs builder Lombok library

I have started using the Lombok library, and I am unable to figure out the difference between using a wither & a builder. @Builder @Wither public class Sample { private int x; private int y; } Now I can create an object in 2 ways: Sample s =…
user1692342
  • 5,007
  • 11
  • 69
  • 128
10
votes
5 answers

Connect a UILabel in Interface Builder and XCode?

I am trying to do something as simple as add a Label to a View in XCode and IB and I can't figure out how to do it. All the samples I find online are for older versions of IB so the directions aren't correct. I have a label on my .xib file, in my…
James P. Wright
  • 8,991
  • 23
  • 79
  • 142
9
votes
2 answers

Flutter - how to call multiple builder items in material app?

class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( builder: BotToastInit(), //1. call BotToastInit navigatorObservers:…
Muhammad Anus
  • 320
  • 4
  • 14
9
votes
3 answers

Lombok's builder with mandatory parameters

If I add @Builder to a class. The builder method is created. Person.builder().name("john").surname("Smith").build(); I have a requirement where a particular field is mandatory. In this case, the name field is mandatory. Ideally, I would like to…
rohit
  • 177
  • 4
  • 12
9
votes
2 answers

How to use Lombok @SuperBuilder on Abstract classes with final fields

Given the following classes with the Lombok annotations @Data and @SuperBuilder @Data @SuperBuilder public abstract class Parent { protected final String userId; protected final Instant requestingTime; } @Data @SuperBuilder public class…
burtmacklin16
  • 715
  • 2
  • 13
  • 31