builder-pattern

How to ensure that builder pattern is completed?

南楼画角 提交于 2019-12-04 10:22:39
问题 EDIT: I am not worried about being called in the wrong order since this is enforced through using multiple interfaces, I am just worried about the terminal method getting called at all. I am using a builder pattern to create permissions in our system. I chose a builder pattern because security is so important in our product (it involves minors so COPPA et al), I felt it was imperative that permissions be readable, and felt that the readability was of the utmost importance (i.e. use a fluent

Java generic builder

末鹿安然 提交于 2019-12-04 03:06:04
Suppose I need some DerivedBuilder to extend some BaseBuilder . Base builder has some method like foo (which returns BaseBuilder ). Derived builder has method bar . Method bar should be invoked after method foo . In order to do it I can override foo method in DerivedBuilder like this: @Override public DerivedBuilder foo() { super.foo(); return this; } The problem is that BaseBuilder has a lot of methods like foo and I have to override each one of them. I don't want to do that so I tried to use generics: public class BaseBuilder<T extends BaseBuilder> { ... public T foo() { ... return (T)this;

How to ensure that builder pattern is completed?

我是研究僧i 提交于 2019-12-03 06:28:47
EDIT: I am not worried about being called in the wrong order since this is enforced through using multiple interfaces, I am just worried about the terminal method getting called at all. I am using a builder pattern to create permissions in our system. I chose a builder pattern because security is so important in our product (it involves minors so COPPA et al), I felt it was imperative that permissions be readable, and felt that the readability was of the utmost importance (i.e. use a fluent-style builder pattern rather than a single function with 6 values). The code looks like this:

Builder pattern vs. config object

大城市里の小女人 提交于 2019-12-02 17:07:26
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; public final float price; private Product(Builder builder) { this.name = builder.name; this.alcohol =

How to use Builder pattern as described by Joshua Bloch's version in my ModelInput class?

做~自己de王妃 提交于 2019-12-02 09:43:55
问题 I am trying to use Builder Pattern for my below class.. Initially I was using constructor of my class to set all the parameters but accidentally I came across Builder pattern and it is looking good for my use case. Below is my class in which people will mostly pass userId , clientId and parameterMap always but other fields are optional and they may or may not pass it. And also if they are not passing any timeout value, I need to have default timeout value set as 500 always but if they are

How to use Builder pattern as described by Joshua Bloch's version in my ModelInput class?

▼魔方 西西 提交于 2019-12-02 02:59:40
I am trying to use Builder Pattern for my below class.. Initially I was using constructor of my class to set all the parameters but accidentally I came across Builder pattern and it is looking good for my use case. Below is my class in which people will mostly pass userId , clientId and parameterMap always but other fields are optional and they may or may not pass it. And also if they are not passing any timeout value, I need to have default timeout value set as 500 always but if they are passing any timeout value then it should override my default timeout value. Here Preference is an ENUM

Builder Pattern: which variant is preferred? [closed]

匆匆过客 提交于 2019-11-30 14:25:08
I was going through Effective Java book , and creating notes for my future reference , i came across Builder Pattern. Well i understood what it is and how its suppose to be used.In the process i created a two example variations of the builder pattern. I would need help in listing down the differences and the advantage each has? Well i certainly noticed that , Example 1 exposes less methods , there by less restrictive and more generic , there by allowing it to be used more flexibly. Please point out other things i have missed? Example 1 package item2; /** * @author Sudhakar Duraiswamy * */

Builder pattern with nested objects

做~自己de王妃 提交于 2019-11-30 07:07:12
Hi I'm stuck with a problem. I want to implement the builder pattern to make creating my objects easier. The problem I face has to do with nested object. The object I would like to create has a list of other objects in it, and I don't really have an idea on how to tackle it. I want to be able to do the following (Simpler objects for example): Receipt RestaurantReceipt = new ReceiptBuilder() .withDate("value") .withName("value") .AddItem("value") .WithIngredients("value") .WithType("value") .AddItem("value") .WithIngredients("value") .WithType("value") .build(); Or something like: Receipt

Builder Pattern: which variant is preferred? [closed]

泄露秘密 提交于 2019-11-29 20:23:15
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I was going through Effective Java book , and creating notes for my future reference , i came across Builder Pattern. Well i

How to clone old builder to make a new builder object?

笑着哭i 提交于 2019-11-29 08:59:15
I have a builder class which I am using in one of my project. Let's say I have metricA as builder based on below class. I need to make a new builder metricB based on metricA by cloning metricA so that metricB contains all the values which were already there in metricA . In the constructor of MetricHolder I am initializing some fields (which are not set directly) basis on fields that have been set already. clientTypeOrPayId - I am initializing this field. If payId is present, then I will set this value or I will set clientType . clientKey - I am initializing this field as well in the same