Last active
January 24, 2022 10:00
-
-
Save anjanashankar9/905cd09c5b65c95529326020dea7183d 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
package builder; | |
public class User { | |
private String firstName; | |
private String lastName; | |
private int age; | |
private String email; | |
private User(UserBuilder builder) { | |
this.firstName = builder.firstName; | |
this.lastName = builder.lastName; | |
this.age = builder.age; | |
this.email = builder.email; | |
} | |
@Override | |
public String toString() { | |
return "User: "+this.firstName+", "+this.lastName+", "+this.age+", "+this.email; | |
} | |
public static class UserBuilder { | |
private final String firstName; | |
private final String lastName; | |
private int age; | |
private String email; | |
public UserBuilder(String firstName, String lastName) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
public UserBuilder age(int age) { | |
this.age = age; | |
return this; | |
} | |
public UserBuilder email(String email) { | |
this.email = email; | |
return this; | |
} | |
public User build() { | |
User user = new User(this); | |
return user; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment