Last active
November 27, 2021 03:07
-
-
Save josemartinez111/a7e39b9a550d2155fe4b34d3a2031c8b to your computer and use it in GitHub Desktop.
A manual example of the builder pattern without Lombok ππ«π«π₯ππ§―
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
| public class BuilderMain { | |
| public static void main(String[] args) { | |
| Employee employee = new Employee.EmployeeBuilder("Cristiano", "Ronaldo", 33, 7) | |
| .setPhone("0045-1234556") | |
| .setAddress("Juventus") | |
| .setMail("CR@Juventus.org").build(); | |
| } | |
| } |
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 main.builderPattern; | |
| public class Employee { | |
| private final String firstName; //required | |
| private final String lastName; //required | |
| private final int age; //required | |
| private final int personalId; // required | |
| private final String phone; //optional | |
| private final String address; //optional | |
| private final String mail; //optional | |
| public static class EmployeeBuilder { | |
| private final String firstName; //required | |
| private final String lastName; //required | |
| private final int age; //required | |
| private final int personalId; // required | |
| private String phone; //optional | |
| private String address; //optional | |
| private String mail; //optional | |
| public EmployeeBuilder(String firstName, String lastName, int age, int personalId) { | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| this.age = age; | |
| this.personalId = personalId; | |
| } | |
| public EmployeeBuilder setAddress(String address) { | |
| this.address = address; | |
| return this; | |
| } | |
| public EmployeeBuilder setPhone(String phone) { | |
| this.phone = phone; | |
| return this; | |
| } | |
| public EmployeeBuilder setMail(String mail) { | |
| this.mail = mail; | |
| return this; | |
| } | |
| public Employee build() { | |
| // call the private constructor in the outer class | |
| return new Employee(this); | |
| } | |
| } | |
| private Employee(EmployeeBuilder builder) { | |
| this.firstName = builder.firstName; | |
| this.lastName = builder.lastName; | |
| this.age = builder.age; | |
| this.personalId = builder.personalId; | |
| this.phone = builder.phone; | |
| this.address = builder.address; | |
| this.mail = builder.mail; | |
| } | |
| public String getFirstName() { | |
| return firstName; | |
| } | |
| public String getLastName() { | |
| return lastName; | |
| } | |
| public int getAge() { | |
| return age; | |
| } | |
| public int getPersonalId() { | |
| return personalId; | |
| } | |
| public String getPhone() { | |
| return phone; | |
| } | |
| public String getAddress() { | |
| return address; | |
| } | |
| public String getMail() { | |
| return mail; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment