Talk:Builder pattern

From Wikipedia, the free encyclopedia

Contents

[edit] Builders and immutable objects

A major benefit of builders is that they can be used to create immutable objects without complex constructors. In Java, the builder pattern also simulates named constructor parameters:

public final class Pizza {
    private final String dough;
    private final String sauce;
    private final String topping;
 
    private Pizza(Builder builder) {
        dough = builder.dough;
        sauce = builder.sauce;
        topping = builder.topping;
    }
 
    public static class Builder {
        private String dough;
        private String sauce;
        private String topping;
 
        public Builder dough(String dough) {
            this.dough = dough; 
            return this;
        }
        public Builder sauce(String sauce) {
            this.sauce = sauce; 
            return this;
        }
        public Builder topping(String topping) {
            this.topping = topping;
            return this;
        }
 
        public Pizza create() {
            return new Pizza(dough, sauce, topping);
        }
    }
}
 
/** A customer ordering a pizza. */
class BuilderExample {
    public static void main(String[] args) {
        Pizza hawaiian = new PizzaBuilder()
             .dough("cross");
             .sauce("mild");
             .topping("ham+pineapple")
             .create();
    }
}

[edit] Abstract Builder class

In the diagram, the Builder should be italicized to indicate that it is an Abstract class. If it is an interface rather than abstract class (alá Java), then it would be better served with an <<interface>> stereotype. —Preceding unsigned comment added by 71.57.242.240 (talk) 22:06, 12 February 2008 (UTC)

[edit] Correspondance between the sequence diagram and the Java program

The Sequence diagram dosen't seems to correspond to the example in Java any toughts ? --Khalid hassani 11:15, 11 August 2006 (UTC)

[edit] Adding a section about the difference between Factory Method pattern and Builder pattern

Many people especially beginners are confused about the difference between those two patterns, as they seem very similar, we need to add a section about the difference between the two patterns, the google groups discussion in the External links section seems a good starting point to me.--Khalid hassani 11:19, 11 August 2006 (UTC)

[edit] Class data members should be private, chefs create pizza

The abstract class PizzaBuilder should not have a protected Pizza data member this should be private. Having protected data creates fragile class hierarchies and generally should be avoided. Also a minor point, but generally chefs do the cooking not waiters!

[edit] Ambiguity? Or am I just confused...

The explanation for the Director class is: "The Director class is responsible for managing the correct sequence of object creation. It receives a Concrete Builder as a parameter and executes the necessary operations on it."

Under the "Difference Between Builder pattern and Abstract factory pattern" heading, this is mentioned: "...the client just requests a configuration and the builder directs the logic of building it"

This seems to say that the Builder manages "the correct sequence of object creation". Is the client the "Director" or is the builder the "Director"? —The preceding unsigned comment was added by 61.14.96.7 (talk) 07:51, 30 April 2007 (UTC).

[edit] Missing the Mark

Previous commentor: You're not confused. The author of this article is deeply confused about the Builder pattern. The sample cited in the first comment above better represents what a Builder is. —Preceding unsigned comment added by Jdmarshall (talk • contribs) 23:19, 17 December 2007 (UTC)

Yeah, this article is totally duff. Where do we get these guys? -- TomAnderson —Preceding unsigned comment added by 128.40.81.110 (talk) 18:24, 12 May 2008 (UTC)

[edit] Most of this page is copied

Most of the text for this page is copied directly from http://sourcemaking.com/design_patterns/builder in violation of its license terms. That work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License. This page does not attribute the work to its original author.

Vaughanje (talk) 19:51, 12 May 2008 (UTC)

Sorry, I don't see the similarity. The only part that is similar is the introductory sentence, which both authors seem to have lifted verbatim from the GoF book, and the secondary concepts, which also, you guessed it, come from the GoF book. Jdmarshall (talk) 11:58, 18 May 2008 (UTC)