Created
November 9, 2022 19:52
-
-
Save rohanjai777/c1b45a7f464aefadc184bd538e00eca0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student{ | |
private String name; | |
private String phone; | |
private String age; | |
public static StudentBuilder getStudentBuilder(){ //to return StudentBuilder object | |
return new StudentBuilder(); | |
} | |
public void print(){ | |
System.out.println(name+" "+phone+" "+age); | |
} | |
static class StudentBuilder{ //called without creating object | |
private Student student = new Student(); //create student object | |
public StudentBuilder setName(String name){ //set name and return builder object | |
student.name = name; | |
return this; | |
} | |
public StudentBuilder setPhone(String phone){ //set phone and return builder object | |
student.phone = phone; | |
return this; | |
} | |
public StudentBuilder setAge(String age){ //set age and return builder object | |
student.age = age; | |
return this; | |
} | |
public Student build(){ //return student object's clone. | |
return student.clone(student); | |
} | |
} | |
public Student clone(Student s){ //just copy one object to another | |
Student student = new Student(); | |
student.name = s.name; | |
student.phone = s.phone; | |
student.age = s.age; | |
return student; | |
} | |
} | |
public class Client{ | |
public static void main(String[] args){ | |
Student.getStudentBuilder() | |
.setName("Rohan") | |
.setPhone("9899899876") | |
.setAge("24") | |
.build() | |
.print(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment