Basically Builder Design pattern is something that is used to create object for classes with more attributes.
Learning this design pattern is so fun, trust me you will say the same once you are done reading this article.
Suppose you are dealing with a class Student which has so many attributes for example studentName, studentAge, studentFatherName and the list goes on..
if you ever encounter this kind of problem in your project just go with builder design pattern as a solution.
class Student{
String stdentName;
int studentId;
int age;
String studentEmail;
String studentUserName;
String studentBatchName;
.....
}
consider the above example, here you would need a constructor with more parameters and it would become difficult when you are creating object using the constructor. Because you would be missing on with some arguments and validating the values passed in constructor, this makes the code not that readable.
Instead, you can take the help of StudentHelper class I mean create a sub-class and that class will do the work for you.
Let me be more clear on the above statement.
Create a subclass StudentHelper, it should be static and should have same attributes as the parent.
class Student{
String stdentName;
int studentId;
int age;
String studentEmail;
String studentUserName;
String studentBatchName;
.....
class static StudentHepler{
String stdentName;
int studentId;
int age;
String studentEmail;
String studentUserName;
String studentBatchName;
}
}
and now create setters for all those attributes, here is the trick!! make them return StudentHelper object.
class Student{
String stdentName;
int studentId;
int age;
String studentEmail;
String studentUserName;
String studentBatchName;
.....
class static StudentHepler{
class Student{
String stdentName;
int studentId;
int age;
String studentEmail;
String studentUserName;
String studentBatchName;
public StudentHelper setStudent(String name){
this.studentName=name;
return this;
}
public StudentHelper setStudentId(int id){
this.studentId=id;
return this;
}
...
}
}
Now create a build method that creates the Student class object validates it and returns.
class Student{
String stdentName;
int studentId;
int age;
String studentEmail;
String studentUserName;
String studentBatchName;
.....
class static StudentHepler{
String stdentName;
int studentId;
int age;
String studentEmail;
String studentUserName;
String studentBatchName;
public StudentHelper setStudent(String name){
this.studentName=name;
return this;
}
public StudentHelper setStudentId(int id){
this.studentId=id;
return this;
}
...
public Student build(){
if(this.age>20){
throw new InvalidInputException("student age must be less than 20")
}
....
//Likewise you can validate all the values.
return new Student(this.stdentName,
this.studentId,
this.age,
this.studentEmail,
this.studentUserName,
this.studentBatchName
);
}
}
}
Now all you need do is create a constructor in the Student class and a static method which will return the StudentHelper class object because using that only you will be able to call all the setters and build a method to create Student class object seamlessly.
class Student{
String stdentName;
int studentId;
int age;
String studentEmail;
String studentUserName;
String studentBatchName;
.....
public Student(String name,int id,int age,String studentEmail,....){
this.name=name;
this.id=id;
this.age=age;
....
}
public StudentHelper getStudentHelper(){
return new StudentHelper();
}
class static StudentHepler{
String stdentName;
int studentId;
int age;
String studentEmail;
String studentUserName;
String studentBatchName;
public StudentHelper setStudent(String name){
this.studentName=name;
return this;
}
public StudentHelper setStudentId(int id){
this.studentId=id;
return this;
}
...
public Student build(){
if(this.age>20){
throw new InvalidInputException("student age must be less than 20")
}
....
//Likewise you can validate all the values.
return new Student(this.stdentName,
this.studentId,
this.age,
this.studentEmail,
this.studentUserName,
this.studentBatchName
);
}
}
}
finally in the client class whenever you want to create the Student's class object any time just call the getStudentHelper method and use StudentHelper's setters and build method. let me show you how.
class Client{
public static void main(String[] args){
StudentHelper studentHelper = Student.getStudentHelper();
Student student = studentHelper.setStudentName("ABC")
.setStudentAge(19)
.setStudentEmail("ABC@gmail.com")
.setStudentBatchName("xyzBatch")
.build();
...
}
}
Just look at the Client code now and how elegant it is..
Let me know what you guys feel in the comments about this "making life easy" design pattern.
Thank you, Bye keep learning and enjoy programming.