builder-pattern

Making Bloch's builder pattern thread-safe: Rechecking necessary in enclosing constructor if NEVER invalid?

杀马特。学长 韩版系。学妹 提交于 2021-01-20 16:52:12
问题 I have recently learned Joshua Bloch's builder pattern for creating objects with many optional fields. I've been using something like it for years, but never used an inner-class until Bloch's book suggested it to me. I love it. I understand that another thread may alter the bulider's configuration, before it's actually built (with build() ), such that it may be necessary to re-validate all values in the constructor of the enclosing class. Below is an example of a builder class that optionally

Making Bloch's builder pattern thread-safe: Rechecking necessary in enclosing constructor if NEVER invalid?

纵饮孤独 提交于 2021-01-20 16:50:49
问题 I have recently learned Joshua Bloch's builder pattern for creating objects with many optional fields. I've been using something like it for years, but never used an inner-class until Bloch's book suggested it to me. I love it. I understand that another thread may alter the bulider's configuration, before it's actually built (with build() ), such that it may be necessary to re-validate all values in the constructor of the enclosing class. Below is an example of a builder class that optionally

How can I build a nested list using builder pattern?

a 夏天 提交于 2021-01-07 06:50:29
问题 I am trying to make a query builder for GraphQL syntax, by using a builder pattern. I already did it for the filtering part: I want to programatically make a way to say what data I want to get back from the query. (NOTE: before the title part of the query was hardcoded into the QueryBuilder. So for that I made a class called Entity which has a list of itself as an attribute. class Entity( private val entity: EntityType, private val entities: MutableList<Entity> = mutableListOf() ) { override

Is it possible to invoke a function with a unaryPlus in kotlin?

纵饮孤独 提交于 2021-01-05 07:19:25
问题 This is a followup question on another question I asked yesterday. How can I build a nested list using builder pattern? Credit to: Pelocho for giving nice answer. I used this Tutorial to do a type safe graphQL query builder: What I want to do now is to simplify what I made And I know that kotlin must have some nice features to do that. Right now I have to invoke functions when I want to add an entity to my query: fun main() { events{ title() // I don't like to do () when it is an edge case }

Is this a valid Java implementation of an immutable class and the Builder pattern?

浪尽此生 提交于 2020-01-02 01:41:13
问题 The Builder implements Cloneable and overrides clone() and instead of copying every field of the builder, the immutable class keeps a private clone of the builder. This makes it easy to return a new builder and create slightly modified copies of an immutable instance. This way I can go MyImmutable i1 = new MyImmutable.Builder().foo(1).bar(2).build(); MyImmutable i2 = i1.builder().foo(3).build(); The Cloneable interface is said to be somewhat broken, but does any of this violate good java

Builder pattern vs. config object

大憨熊 提交于 2019-12-31 08:34:44
问题 The builder pattern is popular to create immutable objects, but there is some programming overhead to create a builder. So I wonder why not simply using a config object. The usage of a builder would look like this: Product p = Product.Builder.name("Vodka").alcohol(0.38).size(0.7).price(17.99).build(); It is obvious that this is very readable and concise, but you have to implement the builder: public class Product { public final String name; public final float alcohol; public final float size;

Usage of abstract class in the builder pattern?

只谈情不闲聊 提交于 2019-12-25 07:43:26
问题 I have two types of payload coming from upstream: It's either PayloadA or PayloadB . There are some common fields between PayloadA and PayloadB so I created Payload class with those common fields and for rest I created two builder class one for each payload. Below is the builder class for PayloadA : public final class PayloadA { private final String clientId; private final String langid; private final String deviceId; private final Map<String, String> applicationPayload; private PayloadA

Improving builder pattern by doing validations at compile time

二次信任 提交于 2019-12-24 12:02:56
问题 I recently started using Builder pattern in one of my projects and I am trying to add some sort of validations on my Builder class. I am assuming we cannot do this at compile time so that's why I am doing this validation at runtime. But may be I am wrong and that's what I am trying to see whether I can do this at compile time. Traditional builder pattern public final class RequestKey { private final Long userid; private final String deviceid; private final String flowid; private final int

How to write an idiomatic build pattern with chained method calls in Rust?

自古美人都是妖i 提交于 2019-12-20 05:49:35
问题 Based on the following examples, its possible to write a build-pattern with chained method calls in Rust which either passes by value or by reference (with a lifetime specifier) Is it possible to create a macro to implement builder pattern methods? How to overload the 'new' method? (top answer) https://github.com/rust-unofficial/patterns/blob/master/patterns/builder.md A builder pattern in Rust may look something like this: ui::Button::new() .label("Test") .align(Align::Center) .build(); When