Adding Data To Objects – Using Builders

Let’s review the problems we faced with the previous two methods of adding data to our Wizard object:

  • The constructor required us to add all of the fields every time we created a new instance of the class, or requires several different constructors. This is fine when there are only one or two different fields – but increasing the number of optional fields exponentially increases the number of constructors required. In general, we don’t really want to have massive constructors with lots of arguments passed in anyway, as it reduces readability and increases complexity, both of which are undesirable as they make the code less maintainable.
  • The setters allowed us to set as many fields on the object as we wanted, but it can make things confusing because we can set things directly onto the object in different places in the code. It is easy to forget or lose track of what has already been set. It is impossible to make the fields immutable with setters, by definition if there is a way to set the data whenever we want to, it is modifiable.

There is a way to add data which solves both of these problems.

The Builder Pattern

A Builder is a separate object altogether, created within the parent class. Its job (as the name suggests) is to build up and return an object of the required type with the correct fields populated. Using a builder results in very readable code – an added bonus! Furthermore, we can make the fields immutable if we want to. I will illustrate both these points by continuing with the example we had previously.

Let’s assume we have a Wizard class, (and for the moment that the fields don’t need to be immutable):

class Wizard {
    private String name;
    private int age;
    private House house;
    private int yearGroup;
    private int apparitionLicenseNumber;

    static class Builder {
        private Wizard wizard = new Wizard();

        public static Builder aWizard() {
            return new Builder();

        public Builder withName(String name) {
   = name;
            return this;

        public Builder withAge(int age) {
            wizard.age = age;
            return this;

        public Builder withHouse(House house) {
   = house;
            return this;

        public Builder withYearGroup(int yearGroup) {
            wizard.yearGroup = yearGroup;
            return this;

        public Builder withApparitionLicenseNumber(int apparitionLicenseNumber) {
            wizard.apparitionLicenseNumber = apparitionLicenseNumber;
            return this;

        public Wizard build() {
            return wizard;

If we then want to construct a Wizard object in another class (e.g. a test class), we can now easily create a very readable block of code which constructs this Wizard object with the attributes we require:

public class WizardTest {
    Wizard harry = aWizard().withName("Harry Potter")

In the next post we will see how we can use a Builder to construct an object with immutable data.



2 thoughts on “Adding Data To Objects – Using Builders

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s