Builder Design Pattern

Builder Design Pattern

There are several design patterns and they have their intent and applicability.

Like - Factory Method, Abstract Factory Method, Builder, Singleton, Prototype.

Why Builder Design Pattern?

A Builder Pattern is a Creational Design Pattern that lets you to construct complex objects step by step.

A Builder Pattern solves the issue with a large number of optional parameters and inconsistent states by providing a way to build the object step-by-step and provide a method that will actually return the final Object.

When We need Builder Design Pattern?

1.Too Many arguments to pass from the client program to the Factory class.

2.Some of the parameters might be optional. In Case of Factory pattern we have to create Separate Constructor in this case ( Constructor Overloading ) or we have to explicitly pass NULL to optional parameters.

3.If the object is heavy and its creation is complex, then all that complexity will be part of Factory classes that may be confusing

Keep in Mind

With This Approach, That Is the Object State Will Be Inconsistent Unless All the Attributes Are Set Explicitly.

Examples of Builder Pattern:

java.lang.StringBuilder and java.lang.StringBuffer has used the builder pattern for object building.

Implementation:

School Class:

public class School {
    private String Name;
    private String Address;
    private Long teachers; // Optional Parameter
    private Long students; // Optional Parameter
    private School(SchoolBuilder schoolBuilder){
        // private so that other class can not directly interact with school constructor.
        this.Name = schoolBuilder.Name;
        this.Address = schoolBuilder.Address;
        this.teachers = schoolBuilder.teachers;
        this.students = schoolBuilder.students;
    }

    public String getName() {
        return Name;
    }

    public String getAddress() {
        return Address;
    }

    public Long getTeachers() {
        return teachers;
    }

    public Long getStudents() {
        return students;
    }

    public static  class SchoolBuilder{
        private String Name;
        private String Address;
        private Long teachers; // Optional Parameter
        private Long students; // Optional Parameter

        public SchoolBuilder(String Name, String Address){
            // constructor for required parameters
            this.Name = Name;
            this.Address = Address;
        }

        public SchoolBuilder setTeachers(Long teachers) {
            this.teachers = teachers;
            return this;
        }

        public SchoolBuilder setStudents(Long students) {
            this.students = students;
            return this;
        }

        public School Build(){
            return new School(this);
        }
    }
}

Main Class:

public class main {
    public static void main(String[] args) {
        // Make a new School Object.
        // School school =  new School("KHHS","Kolkata",2,3); --> Not possible this way.
        School newSchool = new School.SchoolBuilder("KHHS","Kolkata")
                .setStudents(8l)
                .setTeachers(12l)
                .Build();

        System.out.println(newSchool.getName()+" : "+ newSchool.getAddress()+" : "+newSchool.getStudents()+" : "+newSchool.getTeachers());
    }
}