The pattern Builder or Builder is another creational patterns. This pattern lets you separate the construction of a complex object from its representation so that the same construction process can create different representations of this object.
The following image shows the class diagram of this pattern :
- Builder: abstract interface to create products. Concrete
- Builder: Builder implementation, builds and brings together the necessary parts to build the products.
- Director : construct an object using the Builder pattern . Product
- : The complex object under construction.
Below is an example of this pattern, extracted from Wikipedia . In this example we have a kitchen ( Director ) that produces pizzas (Product ) of different characteristics (Concrete Builder ):
/ ** "Product" * /
class Pizza {private String
dough = "" Willow
private String = "";
private String topping = "";
public void
setDough (String dough) {
this.dough = dough;
} public void
setSauce (string sauce) {
this . willow = willow;
} 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");
}
}
/** "Director" */
class Cook {
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();
}
}
/** A given type of pizza being constructed. */
public class BuilderExample {
public static void main(String[] args) {
Cook cook = new Cook();
PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();
PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();
cook.setPizzaBuilder (hawaiianPizzaBuilder)
cook.constructPizza ();
cook.getPizza
Pizza pizza = ();
}}
there who confuses the Builder pattern with Factory, but the difference is that while Factory pattern differs the choice of a particular class of object, the Builder pattern simply hide the logic of creating a complex object.
References:
Builder Pattern in Wikipedia
Builder Builder Pattern, Interfaces Fluid- Design Patterns
Intro to Design Patterns: Builder Pattern
Creational Patterns - Builder Pattern
Design Pattern - Builder Pattern
Other design patterns