Created
February 14, 2023 05:40
-
-
Save PramodDutta/c92dcfadfbb9c1c3d69dbe5201ac9102 to your computer and use it in GitHub Desktop.
Builder Pattern Java - TheTestingAcademy.com
This file contains hidden or 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
| package designpattern.builderPattern; | |
| public class UserCreatorDemo { | |
| public static void main(String[] args) { | |
| User user = new User.UserBuilder("Pramod","Dutta").setAddress("New Delhi").build(); | |
| User user2 = new User.UserBuilder("Aman","Ji").setAge(32).build(); | |
| } | |
| } | |
| class User { | |
| private final String firstName; // required | |
| private final String lastName; // required | |
| private final int age; // optional | |
| private final String phone; // optional | |
| private final String address; // optional | |
| private User(UserBuilder builder) { | |
| this.firstName = builder.firstName; | |
| this.lastName = builder.lastName; | |
| this.age = builder.age; | |
| this.phone = builder.phone; | |
| this.address = builder.address; | |
| } | |
| public String getFirstName() { | |
| return firstName; | |
| } | |
| public String getLastName() { | |
| return lastName; | |
| } | |
| public int getAge() { | |
| return age; | |
| } | |
| public String getPhone() { | |
| return phone; | |
| } | |
| public String getAddress() { | |
| return address; | |
| } | |
| public static class UserBuilder { | |
| private final String firstName; | |
| private final String lastName; | |
| private int age; | |
| private String phone; | |
| private String address; | |
| public UserBuilder(String firstName, String lastName) { | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| } | |
| public UserBuilder setAge(int age) { | |
| this.age = age; | |
| return this; | |
| } | |
| public UserBuilder setPhone(String phone) { | |
| this.phone = phone; | |
| return this; | |
| } | |
| public UserBuilder setAddress(String address) { | |
| this.address = address; | |
| return this; | |
| } | |
| public User build() { | |
| return new User(this); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment