Builder Pattern

The builder pattern is a pattern used to create objects that would have an very large constructor. There are multiple ways people make the builder pattern in Java. We are going to look at two of them. The Wikipedia article shows the classical GoF one.

In our first example we create a simple product class with a builder class and we print it too the console.

public class Product {
    private Long id;
    private String name;
    private double price;

    public Product(Long id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

Our ProductBuilder.

public class ProductBuilder {
    private Long id;
    private String name;
    private double price;

    public ProductBuilder setId(Long id) {
        this.id = id;
        return this;
    }

    public ProductBuilder setName(String name) {
        this.name = name;
        return this;
    }

    public ProductBuilder setPrice(double price) {
        this.price = price;
        return this;
    }

    public Product build() {
        return new Product(id, name, price);
    }
}

Next we print it to the console.

public static void main(String[] args) {

    ProductBuilder builder = new ProductBuilder();
    Product p = builder.setId(1L)
                       .setName("my dindong")
                       .setPrice(22.22)
                       .build();

    System.out.println(p);

    Product p1 = builder.setId(2L)
                        .setPrice(55.22)
                        .build();

    System.out.println(p1);
}

This gives the following output.

Product{id=1, name='my dindong', price=22.22}
Product{id=2, name='my dindong', price=55.22}

In our second example we create a simple product class with a builder as an static inner class and we print it too the console.

public class Product {
    private Long id;
    private String name;
    private double price;

      public static class ProductBuilder{
        private Long id;
        private String name ;
        private double price;

        public ProductBuilder setId(Long id) {
            this.id = id;
            return this;
        }

        public ProductBuilder setName(String name) {
            this.name = name;
            return this;
        }

        public ProductBuilder setPrice(double price) {
            this.price = price;
            return this;
        }

        public Product build() {
            return new Product(this);
        }
    }

    private Product(ProductBuilder builder) {
        this.id = builder.id;
        this.name = builder.name;
        this.price = builder.price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

Next we print it to the console.

Product o = new Product.ProductBuilder()
                            .setId(1L)
                            .setName("Product")
                            .setPrice(22.22)
                            .build();
System.out.println(o);

Product o1 = new Product.ProductBuilder()
                        .setId(1L)
                        .setPrice(22.22)
                        .build();
System.out.println(o1);

This gives the following output.

Product{id=1, name='Product', price=22.22}
Product{id=1, name='null', price=22.22}

With both ways you create a builder, but with the first builder it remembers it’s internals, so you are obligated to re instantiate all attributes. Otherwise the product will have the same attribute value as the last created instance.
With the second builder no internal values are saved so so you don’t need to re instantiate all attributes.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s