Monday, May 13, 2013

Going technical...Builder pattern

"House" when I say this word I know many of you would imagine the place where you are currently living or the place where you lived when you were a kid. It is likely that two different individuals may have two different versions of the house that they are familiar with. But still the basic steps followed for the construction of those houses remain the same. Well now you must be thinking what those basic steps that I am talking about.

When a house is built regardless of the outside appearance of the house one must follow a fundamental sequence of actions to complete the house.

  • 1. Dig the land for foundation
  • 2. Lay the foundation
  • 3. Construct columns and beams
  • 4. Construct side walls or partition walls
  • 5. Fix the roof

In general any house that you can think of must have gone through the above stages of construction. Now that we are clear of the fact that we can generalize the process of constructing a house. We could identify this as a set of stages that must be followed to construct any house.

I guess now you are becoming clearer and clearer of what the title says. "Builder Pattern". So our builder will follow the above sequence and will successfully be able to build a house. If we introduce set of builders which follow the same steps of construction but add some additional touch to the final outcome we could have houses with different appearance.

How are we going to use this in software design. Yes , enough talking start coding then....
public class House {

 private String foundation;
 private String columns;
 private String beams;
 private String walls;
 private String roof;

 public String getFoundation() {
   return foundation;
 public void setFoundation(String foundation) { = foundation;
 public String getColumns() {
   return columns;
 public void setColumns(String columns) {
   this.columns = columns;
 public String getBeams() {
   return beams;
 public void setBeams(String beams) {
   this.beams = beams;
 public String getWalls() {
   return walls;
 public void setWalls(String walls) {
   this.walls = walls;
 public String getRoof() {
   return roof;
 public void setRoof(String roof) {
   this.roof = roof;

public interface HouseBuilder {
 void digFoundation();
 void layFoundation();
 void constructColumnsBeams();
 void constructSideWallsPartitions();
 void fixRoof();
 House completeHouse();

public class CityHouseBuilder implements HouseBuilder {

 private House cityHouse = new House();

 public void digFoundation() {
   System.out.println("Dig foundation using an excavation machine");

 public void layFoundation() {
   System.out.println("Lay foundation using Rubble , Cement and steel");
   cityHouse.setFoundation("Rubble , Cement , Steel");

 public void constructColumnsBeams() {
   System.out.println("Construct columns and beams using steel and concrete");
   cityHouse.setColumns("Concrete , Steel");
   cityHouse.setBeams("Concrete . Steel");

 public void constructSideWallsPartitions() {
   System.out.println("Construct side walls with bricks and cement");
   cityHouse.setWalls("Bricks , Cement");

 public void fixRoof() {
   System.out.println("Fix roof with timber and roof tiles");
   cityHouse.setRoof("Timber , Roof Tiles");

 public House completeHouse() {
   return this.cityHouse;


public class VillageHouseBuilder implements HouseBuilder {

 private House villageHouse = new House();

 public void digFoundation() {
   System.out.println("Dig foundation using hand held tools");

 public void layFoundation() {
   System.out.println("Lay a simple foundation using rubble and clay");
   villageHouse.setFoundation("Rubble and Clay");

 public void constructColumnsBeams() {
   System.out.println("Fix wooded columns and wooden beams");

 public void constructSideWallsPartitions() {
   System.out.println("Cover the side walls with hay and card board");
   villageHouse.setWalls("Hay and Cardboard");

 public void fixRoof() {
   System.out.println("Fix the roof with hay");

 public House completeHouse() {
   return this.villageHouse;


public class Main {

 public static void main(String[] args) {
   System.out.println("Village House :");
   //Build a Village House
   HouseBuilder builder = new VillageHouseBuilder();
   House house = buildHouse(builder);
   System.out.println("City House :");
   //Build a City House
   builder = new CityHouseBuilder();
   house = buildHouse(builder);

 public static House buildHouse(HouseBuilder builder){
   House house = null;
   house = builder.completeHouse();
   return house;


1 comment:

Anonymous said...

Boss.. ur civil engineer by make ?

Need to go beyond patterns.. may b next time u can blog about some real issue u faced n how u solved it using the patterns.. or some way .. :)

Going Technical , Prototype pattern

Well this is the year of 2014 and I have not made a single post yet for this year. Frankly I did not have a good real life example for expla...