There is an abstract class Man, from which the class Worker is inherited. For the Employee class, I create my Builder:

Example:

public abstract class Human { private long id; private Gender gender; private String firstName; private String lastName; public Human() { super(); } public Human(long id, Gender gender, String firstName, String lastName) { this.setId(id); this.gender = gender; this.firstName = firstName; this.lastName = lastName; } } public class EmployeeHuman extends Human { private long id; private Company company; private Appointment appointment; EmployeeHuman() { super(); } EmployeeHuman(final EmployeeHumanBuilder employeeHumanBuilder) { super(employeeHumanBuilder.getId(), employeeHumanBuilder.getGender(), employeeHumanBuilder.getFirstName(), employeeHumanBuilder.getLastName()); this.id = employeeHumanBuilder.getId(); this.company = employeeHumanBuilder.getCompany(); this.appointment = employeeHumanBuilder.getAppointment(); } } public class EmployeeHumanBuilder { private long id; private Gender gender; private String firstName; private String lastName; private Company company; private Appointment appointment; public EmployeeHumanBuilder id(final long id) { this.id = id; return this; } public EmployeeHumanBuilder gender(final Gender gender) { this.gender = gender; return this; } public EmployeeHumanBuilder firstName(final String firstName) { this.firstName = firstName; return this; } public EmployeeHumanBuilder lastName(final String lastName) { this.lastName = lastName; return this; } public EmployeeHumanBuilder company(final Company company) { this.company = company; return this; } public EmployeeHumanBuilder appointment(final Appointment appointment) { this.appointment = appointment; return this; } public long getId() { return id; } public Gender getGender() { return gender; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public Company getCompany() { return company; } public Appointment getAppointment() { return appointment; } public EmployeeHuman build() { return new EmployeeHuman(this); } } 

Question: Is the Pattern Builder implemented correctly? Is he needed here or is this grief from the mind?

  • Do you implement the pattern from GoF? or some other? - Mikhail Vaysman
  • @MikhailVaysman, yes. - studentST
  • open the article and see. Where is your abstract builder? - Mikhail Vaysman
  • @MikhailVaysman, ie for the class Human you need your abstract class Builder? Is a builder needed at all? - studentST
  • GoF builder is not needed here. But the fluent interface (builder) may be appropriate for a large number of different fields. - Mikhail Vaysman

1 answer 1

Builder is not implemented correctly. The EmployeeHuman class should not depend on Builder. It's just that he has many parameters in the constructor, and in order to make them easier to enter, Builder accumulates them, and then creates an object, calling its constructor with these parameters. By the way, this way you can easily create many identical (or almost the same) objects. Here is the correct implementation:

 public class EmployeeHuman extends Human { private Company company; private Appointment appointment; EmployeeHuman() { super(); } EmployeeHuman(long id, Gender gender, String firstName, String lastName, Company company, Appointment appointment) { super(id, gender, firstName, lastName); this.company = company; this.appointment = appointment; } } public class EmployeeHumanBuilder { private long id; private Gender gender; private String firstName; private String lastName; private Company company; private Appointment appointment; public EmployeeHumanBuilder id(final long id) { this.id = id; return this; } public EmployeeHumanBuilder gender(final Gender gender) { this.gender = gender; return this; } public EmployeeHumanBuilder firstName(final String firstName) { this.firstName = firstName; return this; } public EmployeeHumanBuilder lastName(final String lastName) { this.lastName = lastName; return this; } public EmployeeHumanBuilder company(final Company company) { this.company = company; return this; } public EmployeeHumanBuilder appointment(final Appointment appointment) { this.appointment = appointment; return this; } public EmployeeHuman build() { return new EmployeeHuman(id, gender, firstName, lastName, company, appointment); } }