Concepts

Adding Data To Objects – Introduction

Often, we need to create objects that store data. There are a few different ways to do this, and I will be looking at the pros and cons of each in the next few posts.

There are a few considerations we need to think about when deciding what method we want to use:

  • How many fields are there in total that need to be added?
  • Are they all mandatory? If not, what should be the default behaviour if an optional field is not populated?
  • Do we want the data to be immutable? I.e. once it has been set on an object, can it subsequently change?

Let’s assume we have a Wizard class:

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

There are a few ways that we could add this data when we create a new Wizard object:

Add it to the constructor

This method works well if we want the data to be immutable, or if there are only one or two fields in total, and they are both mandatory. However, in our example above this is not the case and there are some complications:

class Wizard {
    …
    public Wizard (String name, int age, House house, int yearGroup, int apparitionLicenseNumber) {
        this.name = name;
        this.age = age;
        this.house = house;
        //… and so on
    }
    …
}

We would then pass the relevant values in when we create a Wizard object (e.g. in the test class):

class WizardTest {
    Wizard harry = new Wizard("Harry Potter", 14, House.GRYFFINDOR, 3, -1);
}

See a problem here? Harry, at 14, is too young to have taken his Apparition test, so he doesn’t have a license number to add onto the object, but because the values are required by the constructor, we pass in a default -1 (which is a bit ugly). If we wish to have some fields that are optional, we need several different constructors with different numbers of arguments based on what should and should not be populated, which can get messy. On the plus side, however, this allows us to make our fields in the object to be immutable, i.e. they can be marked as final and thereafter cannot be changed after the creation of the object.

Let’s explore some other options:

Add setters on to the Wizard class
class Wizard {
    …
    public Wizard () {
        //default constructor
    }
    …

    public setName(String name) {
        this.name = name;
    }

    public setAge(int age) {
        this.age = age;
    }

    public setHouse(House house) {
        this.house = house;
    }

    …

}

In the test:

class WizardTest {
    Wizard harry = new Wizard();
    harry.setName("Harry Potter");
    harry.setAge(14);
    harry.setHouse(House.GRYFFINDOR);
    harry.setYearGroup(3);
}

Here, we don’t have to set the apparition license number explicitly each time we instantiate the object which is good. We can have default behaviour in the Wizard class itself if we wanted to, so that it’s always in one place if it ever needed to change. The issue with this method, however, is that these setters could be invoked in various different places in the test class and can get confusing. Imagine if between setting the age and setting the house we had added a couple of tests. It would be easy to forget that we had set the age previously – or this could have been done by someone else – and we could get behaviour that is confusing. Also, the values are definitely mutable and can therefore be overwritten, which is sometimes, but not always, desirable, and can often lead to mistakes and unwanted behaviour.

There is one other way that is commonly used to add data to objects – the Builder pattern – this will be covered in the next post.

Advertisements

One thought on “Adding Data To Objects – Introduction

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s