Object Builder alternative

Its a fairly common discussion and usually solved by

1. Constructors

MyObject(String name, String blog){
   this.name = name;
   this.blog = blog;
}

Usage:

new MyObject("Satya", "onSoftwareAndStuff");

There is a small issue with it. During creation of the object, the parameters “Satya” and “onSoftwareAndStuff” do not say what they are.  One of the alternatives with dynamic languages is (with Groovy)

 new MyObject(name : "Satya", blog : "onSoftwareAndStuff");

This is much more readable as it says.. name is Satya and blog is onSoftwareAndStuff.

2. Setters

MyObject(){
}
public void setName(String name){
     this.name = name;
}
public void setBlog(String blog){
     this.blog = blog;
}

Usage:

MyObject obj = new MyObject();
obj.setName("Satya");
obj.setBlog("onSoftwareAndStuff");

This is more readable but spreads into multiple lines and repetition of variable “obj” and method ‘set*’ is uncomfortable.

3. Alternatives

To achieve the readability equivalent to the groovy construction, lets try the following alternative:

MyObject(){
}
public MyObject name(String name){
     this.name = name;
     return this;
}
public MyObject blog(String blog){
     this.blog = blog;
     return this;
}

Usage:

MyObject obj = new MyObject().name("Satya").blog("onSoftwareAndStuff");

Probably for a 2 parameter constructor this is an overkill.. but if there are more parameters, this looks a fairly feasible solution.

Any thoughts?

Advertisements

One thought on “Object Builder alternative

  1. There is an issue with your suggested approach which is the object is created even before you have established that all the fields have been initialized correctly.

    What I mean is the following:

    Your code:

    MyObject(String name, String blog){
    this.name = name;
    this.blog = blog;
    }

    would really look like this in practice: {

    MyObject(String name, String blog){
    if(checkStringsEmpty(name, blog)) {
    throw new IllegalArgumentException();
    }
    this.name = name;
    this.blog = blog;
    }

    So, the equivalent Builder that enforces a check can be written as (according to the book effective java):

    class MyObject {

    //….
    //….

    public static class Builder {
    private String name;
    private String blog;

    public Builder() {}

    public Builder name(String val) {
    if(checkEmptyString(val)
    throw new IllegalArgumentException();
    this.name = val; return this;
    }

    public Builder blog(String val) {
    if(checkEmptyString(val)
    throw new IllegalArgumentException();
    this.blog = val; return this;
    }

    public MyObject build() {
    return new MyObject(this);
    }
    }

    public MyObject(Builder b) {
    name = b.name;
    blog = b.blog;
    }

    //getters
    }

    The difference here is that the object is created if and only if all the fields are valid and have been initialized correctly.

    SO you would say:

    MyObject.Builder myB = new MyObject.Builder();
    b.name(“blah”);
    b.blog(“blah”);
    b.build(); //creates the object

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