0

So I have seen a couple of examples on Builder Pattern. In all, The static class or Builder only has those memebers as final that can be intitialized by the constructor. Example -

public class User {
        private final String firstName; // required
        private final String lastName; // required
        private final int age; // 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 static class UserBuilder {
            private final String firstName;
            private final String lastName;
            private int age;

    public UserBuilder(String firstName, String lastName) {
                this.firstName = firstName;
                this.lastName = lastName;
            }

    public UserBuilder age(int age) {
                this.age = age;
                return this;
            }
         }
    }

Why is that only First and Last Name are final that can be intitialized by the constructor? And it cannot be set after that? My doubt is Can't I remove the final from First and Last and have a constructor which initializes all three members and have corresponding setters too in the class?

Vivin
  • 1,327
  • 2
  • 9
  • 28
  • It's no longer a builder if the the members are accessible by setters now is it? – kolossus Nov 25 '14 at 17:34
  • 4
    @kolossus: Uh, yes it is - the whole point of a builder is usually that it's mutable, whereas the built object is immutable. – Jon Skeet Nov 25 '14 at 17:35

1 Answers1

2

The design here is that the first and last names always have to be set, but the age is optional. (I would have used Integer here, rather than defaulting to 0... but then I'd prefer to use a birthDate anyway... but I digress.)

That "required vs optional" part is communicated through forcing the first name and last name to be present on construction... so why would you also want setters? When would you want to set the first name to one thing in the constructor, and then change it?

You could make the fields non-final and add setters... I just don't think it would be a particularly nice thing to do. It's not a matter of anything failing, but just a change in what's communicated through the API design.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks @jon for the answer. I understand your point. But theoretically in case of builder pattern, if we forget about this particular case, do we only intialize only those fields that we want to be required during construction? – Vivin Nov 25 '14 at 17:45
  • @Vwin: I don't understand what you're asking. If everything is optional, then you'd normally have a parameterless constructor - is that what you mean? It's always worth considering what is *truly* optional though. – Jon Skeet Nov 25 '14 at 17:46
  • I am sorry I was confusing. I meant can we have optional parameters too initialized in the constructor? and then have setters for them? Is it a good practice? – Vivin Nov 25 '14 at 18:06