Builder Pattern

November 17th, 2009

Builder pattern is used to construct a complex object step by step and the final step will return the object. The process of constructing an object should be generic so that it can be used to create different representations of the same object.

Complex Object Construction

Complex Object Construction

For example, you can consider construction of a home. Home is the final end product (object) that is to be returned as the output of the construction process. It will have many steps, like basement construction, wall construction and so on roof construction. Finally the whole home object is returned. Here using the same process you can build houses with different properties.

GOF says,

“Separate the construction of a complex object from its representation so that the same construction process can create different representations” [GoF 94]

What is the difference between abstract factory and builder pattern?

Abstract factory may also be used to construct a complex object, then what is the difference with builder pattern? In builder pattern emphasis is on ’step by step’. Builder pattern will have many number of small steps. Those every steps will have small units of logic enclosed in it. There will also be a sequence involved. It will start from step 1 and will go on upto step n and the final step is returning the object. In these steps, every step will add some value in construction of the object. That is you can imagine that the object grows stage by stage. Builder will return the object in last step. But in abstract factory how complex the built object might be, it will not have step by step object construction.

Sample builder design pattern implementation in Java API

DocumentBuilderFactory , StringBuffer, StringBuilder are some examples of builder pattern usage in java API.

Sample Java Source Code for Builder Pattern

Following is the interface, that will be returned as the product from the builder.

package com.javapapers.sample.designpattern.builder;

public interface HousePlan {

	public void setBasement(String basement);

	public void setStructure(String structure);

	public void setRoof(String roof);

	public void setInterior(String interior);
}

Following is the interface for which the factory implementation should be done. Inturn all abstract factory will return this type.

package com.javapapers.sample.designpattern.abstractfactory;

public interface AnimalFactory {
	public Animal createAnimal();
}

Concrete class for the above interface. The builder constructs an implementation for the following class.

package com.javapapers.sample.designpattern.builder;

public class House implements HousePlan {

	private String basement;
	private String structure;
	private String roof;
	private String interior;

	public void setBasement(String basement) {
		this.basement = basement;
	}

	public void setStructure(String structure) {
		this.structure = structure;
	}

	public void setRoof(String roof) {
		this.roof = roof;
	}

	public void setInterior(String interior) {
		this.interior = interior;

	}

}

Builder interface. We will have multiple different implementation of this interface in order to facilitate, the same construction process to create different representations.

package com.javapapers.sample.designpattern.builder;

public interface HouseBuilder {

	public void buildBasement();

	public void buildStructure();

	public void bulidRoof();

	public void buildInterior();

	public House getHouse();
}

First implementation of a builder.

package com.javapapers.sample.designpattern.builder;

public class IglooHouseBuilder implements HouseBuilder {

	private House house;

	public IglooHouseBuilder() {
		this.house = new House();
	}

	public void buildBasement() {
		house.setBasement("Ice Bars");
	}

	public void buildStructure() {
		house.setStructure("Ice Blocks");
	}

	public void buildInterior() {
		house.setInterior("Ice Carvings");
	}

	public void bulidRoof() {
		house.setRoof("Ice Dome");
	}

	public House getHouse() {
		return this.house;
	}
}

Second implementation of a builder. Tipi is a type of eskimo house.

package com.javapapers.sample.designpattern.builder;

public class TipiHouseBuilder implements HouseBuilder {
	private House house;

	public TipiHouseBuilder() {
		this.house = new House();
	}

	public void buildBasement() {
		house.setBasement("Wooden Poles");
	}

	public void buildStructure() {
		house.setStructure("Wood and Ice");
	}

	public void buildInterior() {
		house.setInterior("Fire Wood");
	}

	public void bulidRoof() {
		house.setRoof("Wood, caribou and seal skins");
	}

	public House getHouse() {
		return this.house;
	}

}

Following class constructs the house and most importantly, this maintains the building sequence of object.

package com.javapapers.sample.designpattern.builder;

public class CivilEngineer {

	private HouseBuilder houseBuilder;

	public CivilEngineer(HouseBuilder houseBuilder){
		this.houseBuilder = houseBuilder;
	}

	public House getHouse() {
		return this.houseBuilder.getHouse();
	}

	public void constructHouse() {
		this.houseBuilder.buildBasement();
		this.houseBuilder.buildStructure();
		this.houseBuilder.bulidRoof();
		this.houseBuilder.buildInterior();
	}
}

Testing the sample builder design pattern.

package com.javapapers.sample.designpattern.builder;

public class BuilderSample {
	public static void main(String[] args) {
		HouseBuilder iglooBuilder = new IglooHouseBuilder();
		CivilEngineer engineer = new CivilEngineer(iglooBuilder);

		engineer.constructHouse();

		House house = engineer.getHouse();

		System.out.println("Builder constructed: "+house);
	}
}

Output of the above sample program for builder pattern

Builder constructed: com.javapapers.sample.designpattern.builder.House@7d772e

Nice example dude, thank you :)

Muhammad Ghazali on December 4th, 2009 5:43 pm

The way you its demonstrated is really amazing.
Thank You dude

Thangarasu Perumal on December 9th, 2009 6:32 pm

Your example helped me to Visualize the Pattern very easily

Thanks,
Ragu

Ragu on December 14th, 2009 9:30 pm

Thanks for this, I had always wondered what the difference between Abstract Factory and Builder was.

Phil on December 16th, 2009 3:52 pm

Good post. Note that returning ‘this’ from each setXXX() method on the builder allows for the user to string the building process together in a very fluent IDE-friendly style. Eg:

House h = new HouseBuilder()
.setRoof()
.setWalls()
.setFloor()
.build();

Kit on December 16th, 2009 10:26 pm

Excelent post!

SMiGL on December 21st, 2009 9:07 am

@Kit: Yup, +1 for you!

Rahul Batra on December 25th, 2009 10:35 am

Keep up good work, exlpanined very well

Hiren on February 23rd, 2010 1:17 am





hidden and gravatar enabled.