As far as I know about builder pattern, for example, an object, Student:
public class Student{
private String name;
private int age;
//setter and getter
}
to apply builder pattern to Student, StudentBuilder should be like this:
Version 1 : Set Student as an instance variable of StudentBuilder
public class StudentBuilder{
private Student _student;
public void setName(String name){
this._student.setName(name);
}
public void setAge(int age){
this._student.setAge(age);
}
public Student build(){
return this._student;
}
}
However, after reading question and answer in this : https://softwareengineering.stackexchange.com/a/388062, as I know, the builder in the question uses the instance variables as parameters, like the following shows:
Version 2 : Set all the parameters of Student as instance variables of StudentBuilder first, finally new Student() and assign the parameters to Student:
public class StudentBuilder{
private String _name;
private int _age;
public void setName(String name){
this._name=name;
}
public void setAge(int age){
this._age(age);
}
public Student build(){
Student student=new Student();
student.setName(_name);
student.setAge(_age);
return student;
}
}
My question is, version 1 and 2, which is the proper builder pattern?