Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java best way to implement builder pattern

Tags:

java

builder

Which of the following is the better approach to implement the builder pattern?

1) Using the object to build instead of all its properties in the builder (and create it in the builder constructor):

public class Person {
    private String firstName;
    // other properties ...

    private Person() {}

    // getters ...

    public static class Builder {
        // person object instead of all the person properties
        private Person person;

        public Builder() {
            person = new Person();
        }

        public Builder setFirstName(String firstName) {
            person.firstName = firstName;

            return this;
        }

        // other setters ...

        public Person build() {
            if (null == person.firstName) {
                throw new IllegalStateException("Invalid data.");
            }

            return person;
        }
    }
}

2) Using the properties of the object to build instead of the object directly in the builder (and create it in the build() method):

public class Person {
    private String firstName;
    // other properties ...

    private Person() {}

    // getters ...

    public static class Builder {
        // person properties instead of object
        private String firstName;
        // other properties ...

        public Builder() {}

        public Builder setFirstName(String firstName) {
            this.firstName = firstName;

            return this;
        }

        // other setters ...

        public Person build() {
            if (null == this.firstName) {
                throw new IllegalStateException("Invalid data.");
            }

            Person person = new Person();
            person.firstName = firstName;

            return person;
        }
    }
}

I prefer the first way because i think that with lots of properties repeat them in the builder is redundant. Are there some disadvantages with the first approach?

Thanks in advance and sorry for my bad english.

like image 479
raxell Avatar asked Apr 19 '14 11:04

raxell


People also ask

When should we use builder pattern in java?

The builder pattern simplifies the creation of objects. It also simplifies the code as your do not have to call a complex constructor or call several setter methods on the created object. The builder pattern can be used to create an immutable class.

When would you apply the Builder design pattern?

Builder pattern aims to “Separate the construction of a complex object from its representation so that the same construction process can create different representations.” It is used to construct a complex object step by step and the final step will return the object.


1 Answers

Small Note : Yes the properties might be a repeat but they have advantages

Details below : If you look at the details here.

Pizza pizza = new Pizza(12);
pizza.setCheese(true);
pizza.setPepperoni(true);
pizza.setBacon(true);

The problem here is that because the object is created over several calls it may be in an inconsistent state partway through its construction. This also requires a lot of extra effort to ensure thread safety.

The better alternative is to use the Builder Pattern.

Notice below method in Builder and respective constructor or parent Pizza class - full code in link here

 public static class Builder {

    public Pizza build() {    // Notice this method
      return new Pizza(this);
    }
  }

  private Pizza(Builder builder) {  // Notice this Constructor
    size = builder.size;
    cheese = builder.cheese;
    pepperoni = builder.pepperoni;
    bacon = builder.bacon;
  }
like image 161
Javaboy Avatar answered Sep 27 '22 02:09

Javaboy