Tuesday, 5 August 2014

Learn A Pattern A Week With Pie - Builder Pattern

Welcome back to our series on design patterns.

This week our focus will be on the Builder pattern, a creational pattern which provides ways to construct a complex object one step at a time.

So let's say you've a class called Student with a constructor which accepts 3 parameters; firstname, lastname and department. Obviously, creating an object of our hypothetical Student class is a no brainer. Just pull out your editor and fondle some few keys to get it done as shown below:

Student s = new Student("Edward","Pie","Computer Science");

The above snippet is easy enough to be memorised by a day old chick, isn't it?

What if you'd a constructor which accepts 10,15,20, or more parameters? Would you still go by the strategy above? Yes or No? Be honest!

Wait babe, don't flush in 10,15,20 or more arguments into your constructor. That's gluttony and you know that's unprofessional. And now you're wondering, "what else can I do?" Uuhhhhm! Ok lemme create a parameterless constructor and throw in a bunch of setter methods to set properties on the object.
Really? Mr. holy sinner, there's a better way (ask your parents).

Aaaah! I caught you there, now you're scratching your head and you're saying "what the heck am I to do?" Tadaaaaah! The Builder Pattern says, "hey! I am every heck you've to do"

The Builder Pattern

This is a pattern for constructing complex objects in a step-by-step approach using simple objects. In this pattern, a Builder class which is independent of other objects builds the final object in succession of steps.
This pattern falls under the creational pattern in the GoF's grouping of design patterns.

Ti's Time For Hands-On

Let's have a practice lab session and try out one of the simple ways of implementing the builder pattern.
We'll continue working with our hypothetical Student class, this time it accepts 6 parameters instead of 3. Six parameters aren't too much to be supplied as arguments to a constructor but we're sticking to 6 parameters just so it'll be easier to understand. You'll be able implement a Builder for a much complex object after understanding this.

The 6 parameters for our hypothetical Student class are firstname,lastname,department,cwa,level, and hallOfResidence.

See a simple implementation of the above scenario below:



I admit you've to add extra code to implement the builder pattern for such a simple scenario but the focus is on being able to construct complex objects in a step-by-step approach. With the builder pattern, you're able to chain a series of setter methods calls with each call taking the object under construction a step further.

Consider a typical real world situation where you take your car to the car repairs shop, the fixing of your car's problems may start with the welder welding broken pieces together, the auto-electrician fixing electrical problems, the mechanic fixing your engine and finally the sprayer giving your car a new look. You'll agree with me that the movement of your car from one expert to the other takes it closer to your final goal, fixing your car. This is how the builder pattern works. It helps you to construct complex objects in a step-by-step manner.

There are other ways of implementing a builder but for the sake of understanding I chose a very simple approach.

Your comments, suggestions, questions and criticisms are welcomed here or on Facebook.

Thanks for learning with me. See you in the next post!

Remember to have fun!!!




5 comments:

  1. I have always been wondering how the bulder pattern is done! thankx for the insight!! I am gonna use it asap!!! I suggest that make pdf copies available!

    ReplyDelete
  2. I think we have to reconsider the naming for the so called "setters" in the StudentBuilder inner class....because in this case they are behaving more like both setters and getters..

    ReplyDelete
    Replies
    1. That is how it has to be in order for method chaining to be possible.

      Delete
  3. Great Insight... Loving the design pattern series

    ReplyDelete