0

I came across a blog on medium which highlights the use of builder design pattern. First they create a person POJO class

public class Person {

    private final String firstName;
    private final String lastName;
    private final int age;
    private final String height;
    private final String weight;
    private final String eyesColor;
    private final String hairColor;
    private final String birthPlace;
    private final Date birthDate;
    private final int numberOfSibling;
    private final boolean married;
   // Private Constructor
    private Person(PersonBuilder builder){
        this.firstName = builder.firstName;
        this.lastName = builder.lastName;
        this.age = builder.age;
        this.height = builder.height;
        this.weight = builder.weight;
        this.eyesColor = builder.eyesColor;
        this.hairColor = builder.hairColor;
        this.birthPlace = builder.birthPlace;
        this.birthDate = builder.birthDate;
        this.numberOfSibling = builder.numberOfSibling;
        this.married = builder.married;
    }

    // Static Inner Class with same parameters
    public static class PersonBuilder{

        private String firstName;
        private String lastName;
        private int age;
        private String height;
        private String weight;
        private String eyesColor;
        private String hairColor;
        private String birthPlace;
        private Date birthDate;
        private int numberOfSibling;
        private boolean married;
        public PersonBuilder(){}
/* If you have some necessary/compulsary parameters then you can use parametrized constructor instead of default constructor.
        public PersonBuilder(String firstName){
            this.firstName = firstName;
       }  */
        public PersonBuilder setFirstName(String firstName){
            this.firstName = firstName;
            return this;
        }

        public PersonBuilder setLastName(String lastName){
            this.lastName = lastName;
            return this;
        }

        public PersonBuilder setAge(int age){
            this.age = age;
            return this;
        }

        public PersonBuilder setHeight(String height){
            this.height = height;
            return this;
        }

        public PersonBuilder setWeight(String weight){
            this.weight = weight;
            return this;
        }

        public PersonBuilder setEyesColor(String eyesColor){
            this.eyesColor = eyesColor;
            return this;
        }

        public PersonBuilder setHairColor(String hairColor){
            this.hairColor = hairColor;
            return this;
        }

        public PersonBuilder setBirthPlace(String birthPlace){
            this.birthPlace = birthPlace;
            return this;
        }

        public PersonBuilder setBirthDate(Date birthDate){
            this.birthDate = birthDate;
            return this;
        }

        public PersonBuilder setNumberOfSibling(int numberOfSibling){
            this.numberOfSibling = numberOfSibling;
            return this;
        }

        public PersonBuilder setMarried(boolean married){
            this.married = married;
            return this;
        }

        public Person build(){
            Person person = new Person(this);
            return person;
        }
    }
}

They then create an object by invoking the build method as :-

Person person = new Person.PersonBuilder()
                .setFirstName("Gaurav Tyagi")
                .setAge(30)
                .setBirthPlace("India")
                .setMarried(true)
                .setNumberOfSibling(2)
                .build();

I want to know what benefit does this builder design pattern offer. I can create Person class as -

public class Person{
 private final String firstName;
    private final String lastName;
    private final int age;
//and other properties
public Person(){}
public Person setAge(int age)
{
this.age=age;
return this;
} 
// and other setters returning Person object
}

and create object

Person person=new Person().setAge(20).setName("Samarth"); // and other setters chained together.

I can get similar results and I even don't need the build method and also don't need to create a nested static class with repeated properties. Correct me if I am wrong!

LFC
  • 50
  • 5
  • The first line in the blog post you linked to has a pretty good answer.. _"The Builder pattern is a creational design pattern that lets you construct complex objects step by step."_ – Kaan Jul 08 '22 at 04:06
  • Design patterns are not obligatory. If you don't think a given design pattern will help your particular use-case ... don't use it. The example above is one where the benefit is marginal. But in other use-cases, the builder pattern is helpful. – Stephen C Jul 08 '22 at 05:00

0 Answers0