tag:blogger.com,1999:blog-8712770457197348465.post3749302985247516036..comments2024-03-19T01:04:48.783-07:00Comments on Javarevisited: Builder Design pattern in Java - Example Tutorialjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger27125tag:blogger.com,1999:blog-8712770457197348465.post-2207766518100859682019-06-26T06:47:16.519-07:002019-06-26T06:47:16.519-07:00Oh, they did. Wish I could retract my comment :-(Oh, they did. Wish I could retract my comment :-(Paul Coombeshttps://www.blogger.com/profile/07909309399202235760noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-43078947798558884542019-06-26T06:46:30.675-07:002019-06-26T06:46:30.675-07:00Nice article and it explained a lot for more.
Surp...Nice article and it explained a lot for more.<br />Surprisd no one noticed the following typo:<br /><br />public Builder bakingpowder(double spoon){this.sugar = spoon; return this; }<br /><br />Paul Coombeshttps://www.blogger.com/profile/07909309399202235760noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-72937451324117451332016-08-25T04:17:41.818-07:002016-08-25T04:17:41.818-07:00Joshua Bloch gave this example in his excellent bo...Joshua Bloch gave this example in his excellent book and mentioned that this pattern is similar to GoF Builder in his excellent book.<br /><br />And after that, all the bloggers and people who love superficiality of things took Bloch's example, changed it one way and another and presented it as Builder Pattern.<br /><br />Folks, this is enough. Don't represent this as a Builder Pattern Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-31903290177809882172016-06-16T04:48:56.870-07:002016-06-16T04:48:56.870-07:00@Unknown, good spot, bakingPowder is never assigne...@Unknown, good spot, bakingPowder is never assigned value :-), will correct it. Thanksjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-13707400286551960682016-06-15T12:38:39.378-07:002016-06-15T12:38:39.378-07:00Nice, but your cake won't rise without backing...Nice, but your cake won't rise without backing powder. Check your bakingpowder() method.Russell Batemanhttps://www.blogger.com/profile/06491108021827605162noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-90200483588973469742016-05-07T03:08:12.161-07:002016-05-07T03:08:12.161-07:00why dont we have those setter and build methods in...why dont we have those setter and build methods inside Cake, why we need static nested class here.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-42692999763382411002016-05-07T03:05:47.971-07:002016-05-07T03:05:47.971-07:00What if i use, setters after creating the CAKE obj...What if i use, setters after creating the CAKE object, not getting "leaving Object on inconsistent state during building". What happens if create the CAKE first then i add the ingredients to it. I know in real time it is not possible.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-76424212752621971232015-07-11T00:18:01.331-07:002015-07-11T00:18:01.331-07:00Hello @Jebin Joshep. If you do that you are not us...Hello @Jebin Joshep. If you do that you are not using a Builder pattern, because your Cake object is created even before calling a build() method, also the fluency of code will lost e.g. its not possible to create object with configuration in one line.<br /><br />In Builder pattern : <br />Cake whiteCake = new Cake.Builder().sugar(1).butter(0.5). eggs(2).vanila(2).flour(1.5). bakingpowder(0.75)javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-30418303246623966482015-07-09T12:27:52.334-07:002015-07-09T12:27:52.334-07:00Can i remove the member variables in the Builder w...Can i remove the member variables in the Builder with a Cake variable and have the builder methods like eggs, butter, milk set the value on the Cake variable? Is it a good approach? Or am I doing something very wrong?<br /><br />public static class Builder {<br /><br /> private Cake cake;<br /> <br /> public Builder() {<br /> cake = new Cake();<br /> }<br /><br /><br /> public Builder Anonymoushttps://www.blogger.com/profile/10245643810234981768noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-37951618867275679212015-01-15T23:29:20.840-08:002015-01-15T23:29:20.840-08:00Hi Javin,
I guess your pattern panned out very w...Hi Javin, <br /><br />I guess your pattern panned out very well. But I want you to elaborate more on the return types. I feel their use without explanation in this pattern, it seems, both mystery and indulgence- on your part- to me. Your answer will offer a better perspective for a beginner like me.<br /><br />Thanks and Well appreciated<br /><br /><br /><br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55679222871987942712014-12-17T07:40:17.944-08:002014-12-17T07:40:17.944-08:00I really like your technique of the embedded build...I really like your technique of the embedded builder object and it's fluent (as in Google's fluent) style of taking parameters and using them to build the final instance of the object. That's neat.<br /><br />However, this version of the "builder" pattern is pretty different from the builder pattern that you would see in the original Gang of Four book. There they view the Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-21542697873096451852014-08-26T12:20:17.702-07:002014-08-26T12:20:17.702-07:00why like this
public Builder bakingpowder(double s...why like this<br />public Builder bakingpowder(double spoon){<br />this.sugar = spoon; <br />return this; <br />}<br />not like this:<br /><br />public Builder bakingpowder(double spoon){<br />this.bakingpowder = spoon; <br />return this; <br />}<br /><br />thanksAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-63254349091750939692014-05-13T09:44:35.137-07:002014-05-13T09:44:35.137-07:00This is really a awesome blog. The example and the...This is really a awesome blog. The example and the explanation is really appreciable. Thanks for providing tutorial on builder design pattern.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-79471138278479177022014-02-09T13:43:22.789-08:002014-02-09T13:43:22.789-08:00If there are defaults that are included for every ...If there are defaults that are included for every instance of cake that is to be returned, then change the access modifiers of all required methods to private, and include them in the method body of the build() method. That way, whenever build() is invoked, there will be a default object initialized. Also, since those methods are private but included in the build() method, the method chaining tehemperorerhttps://www.blogger.com/profile/07252937286298358376noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-4688842498341847502014-01-24T07:07:23.956-08:002014-01-24T07:07:23.956-08:00another drawback is you can't debug easily.
ht...another drawback is you can't debug easily.<br />http://en.wikipedia.org/wiki/Fluent_interface#ProblemsAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14107893925382348032014-01-03T04:33:19.876-08:002014-01-03T04:33:19.876-08:00Sorry, this one is Telescopic pattern not template...Sorry, this one is Telescopic pattern not template pattern.Satyanoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-34821259233053581702014-01-01T14:28:11.150-08:002014-01-01T14:28:11.150-08:00This is called Template pattern. You can see the s...This is called Template pattern. You can see the same example in Effective java book. Satyanoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-20628461162322778812013-06-29T17:32:47.598-07:002013-06-29T17:32:47.598-07:00What's the point in complaining about whether ...What's the point in complaining about whether this is strictly a Builder or not? The version here could be called a "Light Builder" that combines Director and Product but still reduces errors and makes code more fluent.My thanks to the author.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-37169062446961269622013-06-18T23:52:37.374-07:002013-06-18T23:52:37.374-07:00Hey man! this is not the builder pattern! Study Go...Hey man! this is not the builder pattern! Study GoF please! Pensierohttps://www.blogger.com/profile/04590005475410388188noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-46509241761301131812013-06-18T13:04:00.236-07:002013-06-18T13:04:00.236-07:00I'm with JohnMunsch, even if your code is valu...I'm with JohnMunsch, even if your code is valuable, don't call it builder pattern.<br />The examples on wikipedia are the builder pattern:<br />http://en.wikipedia.org/wiki/Builder_pattern<br /><br />In your code; it is a builder, not a pattern. chezlesbelgeshttps://www.blogger.com/profile/01172134293444473639noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-1825767137440450412013-06-18T02:49:03.402-07:002013-06-18T02:49:03.402-07:00This is a variant of Fluent Interface(http://marti...This is a variant of Fluent Interface(http://martinfowler.com/bliki/FluentInterface.html) used in DSL.<br /><br />There is no need to copy all the fields from Cake to Builder class. Just keep an instance of Cake in Builder class.Anto Paulhttps://www.blogger.com/profile/02363611677084975993noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-66749706687795747032013-06-09T13:44:05.505-07:002013-06-09T13:44:05.505-07:00I see "this" is returned from all builde...I see "this" is returned from all builder-parts, but how does that work, the builder is a static class and static classes can't have a "this" reference. So what am I missing.<br />(I understand the pattern otherwise, it's just the "this" reference I don't get=Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-25540908941286062482013-05-24T20:44:21.077-07:002013-05-24T20:44:21.077-07:00Hi moshii, totally agree with you , a class diagra...Hi moshii, totally agree with you , a class diagram will certainly helps to understand design pattern better. Noted and will add a class diagram here. ThanksJavin @ Must Override super class method Errorhttp://javarevisited.blogspot.com/2013/02/must-override-superclass-method-java-eclipse.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-29159366561139264192012-08-20T06:10:06.857-07:002012-08-20T06:10:06.857-07:00@Anonymous and @Javin Paul
What if, instead of thr...@Anonymous and @Javin Paul<br />What if, instead of throwing exceptions, the API is done as such that the builder dictates which method is possible to call or not? <br />I described how, using the step builder pattern, in an article here : http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html <br />marcocasthttps://www.blogger.com/profile/09589835245958019484noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-10138706685719012992012-07-16T07:56:21.890-07:002012-07-16T07:56:21.890-07:00@Anonymous, You have raised a valid scenario, If A...@Anonymous, You have raised a valid scenario, If Any object has combination of mandatory and optional parameter than before building, build method can check if those parameters are provided or not and it can throw Exception, similar to IllegalArgumentException.<br /><br />JavinJavin Paulhttp://javarevisited.blogspot.sg/2011/09/difference-vector-vs-arraylist-in-java.htmlnoreply@blogger.com