TechnologyZer
technologyzer.com

Builder Design Pattern

Builder design pattern is a Creational design pattern used in object-oriented programming. It helps construct complex objects step by step.

Imagine you are tasked with building a house out of LEGO bricks. Each LEGO piece represents a part of the house, like walls, windows, doors, and roof. Now, think about how you would build the house:

  1. You start with the base of the house, then add walls, windows, doors, and roof one by one.
  2. You can customize your house by choosing different types of walls, windows, and doors, depending on how you want your house to look.

Now, let’s relate this to the Builder design pattern:

In programming, the Builder design pattern helps us construct complex objects step by step. Each step corresponds to adding a part of the object, just like adding LEGO pieces to build the house.

Here’s how it works:

  1. We have a Director, who knows how to build the object step by step. In our LEGO house example, the Director would be like someone who gives you instructions on how to build the house.
  2. We have a Builder, which is responsible for creating the different parts of the object. In our LEGO house example, the Builder would be like you, who follows the instructions to add walls, windows, doors, and roof.
  3. Once all the parts are added, the Builder constructs the final object. In our LEGO house example, this would be when you finish building the house according to the instructions.
public class Student {
    private String firstName;
    private int age;
    private String major;
    private String university;

    public Student(StudentBuilder studentBuilder){
        this.firstName = studentBuilder.firstName;
        this.age = studentBuilder.age;
        this.major = studentBuilder.major;
        this.university = studentBuilder.university;

    }

    @Override
    public String toString() {
        return "Student{" +
                "firstName='" + firstName + '\'' +
                ", age=" + age +
                ", major='" + major + '\'' +
                ", university='" + university + '\'' +
                '}';
    }

    //builder  class
    static class StudentBuilder {
        private String firstName;
        private int age;
        private String major;
        private String university;

        public StudentBuilder setFirstName(String firstName) {
            this.firstName = firstName;
            return this;
        }

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

        public StudentBuilder setMajor(String major) {
            this.major = major;
            return this;
        }

        public StudentBuilder setUniversity(String university) {
            this.university = university;
            return this;
        }

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


class Director{
    Student.StudentBuilder studentBuilder;
    Director(Student.StudentBuilder studentBuilder){
        this.studentBuilder = studentBuilder;
    }
    public Student constructStandardStudent() {
        return studentBuilder
                .setFirstName("Sachin")
                .setAge(20)
                .setMajor("Computer Science")
                .setUniversity("XYZ University")
                .build();
    }

    public Student constructCustomStudent(String firstName, int age, String major, String university) {
        return studentBuilder
                .setFirstName(firstName)
                .setAge(age)
                .setMajor(major)
                .setUniversity(university)
                .build();
    }
}

public class Main {
    public static void main(String[] args) {
        Student.StudentBuilder studentBuilder = new Student.StudentBuilder();
        Director director = new Director(studentBuilder);
        Student standardStudent = director.constructStandardStudent();
        System.out.println("Standard Student: " + standardStudent);
        Student customStudent = director.constructCustomStudent("saurav", 23, "IT", "ABC University");
        System.out.println("Custom Student: " + customStudent);
    }
}

Leave a Comment