Saturday, September 15, 2007

Builder pattern

Today i want to introduce Builder Pattern.

1. First we need to know the Builder Pattern class diagram:
Class diagram:



Now we will explain the meaning of those class:

Builder: An interface used to create complex object.

Concrete Builder: An implementation of Builder. Builder give us the way to construct object. And Concrete Builder show us how to do in detail.

Director: The Director class is responsible for managing the correct sequence of object creation. It receives a Concrete Builder as a parameter and executes the necessary operations on it.

Explanation:
We have a complex object with many part. To create object, we have to create many sub item in it. For example: We have a pizza. There are many type of pizza. Each type has its own way to do pizza.
For Hawaiian pizza: it make from cross, mild, ham+pineapple
For Spicy pizza: it make from pan baked, hot, pepperoni+salami


So we design as below:
+ Builder define how to create pizza. It provide the mean to create pizza:
public void setDough(String dough)
public void setSauce(String sauce)
public void setTopping(String topping)

For each kind of pizza( a concrete builder) , we will give pizza a suitable thing




/** "Product" */
class Pizza
{
private String dough = "";
private String sauce = "";
private String topping = "";

public void setDough(String dough)
{ this.dough = dough; }
public void setSauce(String sauce)
{ this.sauce = sauce; }
public void setTopping(String topping)
{ this.topping = topping; }
}

/** "Abstract Builder" */
abstract class PizzaBuilder
{
protected Pizza pizza;

public Pizza getPizza()
{
return pizza;
}
public void createNewPizzaProduct()
{
pizza = new Pizza();
}

public abstract void buildDough();
public abstract void buildSauce();
public abstract void buildTopping();
}

/** "ConcreteBuilder" */
class HawaiianPizzaBuilder extends PizzaBuilder
{
public void buildDough()
{
pizza.setDough("cross");
}
public void buildSauce()
{
pizza.setSauce("mild");
}
public void buildTopping()
{
pizza.setTopping("ham+pineapple");
}
}

/** "ConcreteBuilder" */
class SpicyPizzaBuilder extends PizzaBuilder
{
public void buildDough()
{
pizza.setDough("pan baked");
}
public void buildSauce()
{
pizza.setSauce("hot");
}
public void buildTopping()
{
pizza.setTopping("pepperoni+salami");
}
}

But we need to describe the sequence to do pizza(step by step)
so we need to implement Director and give it Builder as parameter


class Waiter
{
private PizzaBuilder pizzaBuilder;

public void setPizzaBuilder(PizzaBuilder pb)
{
pizzaBuilder = pb;
}
public Pizza getPizza()
{
return pizzaBuilder.getPizza();
}

public void constructPizza()
{
pizzaBuilder.createNewPizzaProduct();
pizzaBuilder.buildDough();
pizzaBuilder.buildSauce();
pizzaBuilder.buildTopping();
}
}


Finally: We use it:

class BuilderExample
{
public static void main(String[] args)
{
Waiter waiter = new Waiter();
PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();
PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();

waiter.setPizzaBuilder( hawaiianPizzaBuilder );
waiter.constructPizza();

Pizza pizza = waiter.getPizza();
}
}

Hope it can help you to understand builder pattern

Google