📢 This article was translated by gemini-3-flash-preview
Builder Pattern - Creational Pattern
Intent
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
Structure

Roles:
- Builder: Specifies an abstract interface for creating parts of a Product object.
- ConcreteBuilder: Implements the Builder interface to construct and assemble parts of the product. It defines and tracks the representation it creates and provides an interface for retrieving the product.
- Director: Constructs an object using the Builder interface.
- Product: Represents the complex object being built. ConcreteBuilder creates the internal representation and defines the assembly process. It includes classes that define the components and the interfaces to assemble them.
Applicability
Use the Builder pattern when:
- The algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled.
- The construction process must allow for different representations of the constructed object.
Example 1
A fast-food restaurant makes kids’ meals, usually consisting of a main course (various pizzas), a drink, and a toy. While the specific items vary, the preparation process is identical. A Waiter (Director) coordinates the chef to prepare the meal. Here is the implementation using the Builder pattern.

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| class Pizza {
private String parts;
public void setParts(String parts) { this.parts = parts; }
public String toString() { return this.parts; }
}
abstract class PizzaBuilder {
protected Pizza pizza;
public Pizza getPizza() { return pizza; }
public void createNewPizza() { pizza = new Pizza(); }
public abstract void buildParts();
}
class HawaiianPizzaBuilder extends PizzaBuilder {
public void buildParts(){
pizza.setParts("cross + mild + ham&pineapp1e");
}
}
class SpicyPizzaBuilder extends PizzaBuilder {
public void buildParts() {
pizza.setParts("panbaked + hot + pepperoni&salami");
}
}
class Waiter {
private PizzaBuilder pizzaBuilder;
public void setPizzaBuilder(PizzaBuilder pizzaBuilder) { /* Set the builder */
this.pizzaBuilder = pizzaBuilder;
}
public Pizza getPizza() { return pizzaBuilder.getPizza(); }
public void construct() { /* Build */
pizzaBuilder.createNewPizza();
pizzaBuilder.buildParts();
}
}
class FastFoodOrdering {
public static void main(String[] args) {
Waiter waiter = new Waiter();
PizzaBuilder hawaiian_pizzabuilder = new HawaiianPizzaBuilder();
waiter.setPizzaBuilder(hawaiian_pizzabuilder);
waiter.construct();
System.out.println("Pizza: " + waiter.getPizza());
// Output: Pizza: cross + mild + ham&pineapp1e
}
}
|
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| #include <iostream>
#include <string>
using namespace std;
class Pizza
{
private:
string parts;
public:
void setParts(string parts) { this->parts = parts; }
string getParts() { return parts; }
};
class PizzaBuilder
{
protected:
Pizza *pizza;
public:
Pizza *getPizza() { return pizza; }
void createNewPizza() { pizza = new Pizza(); }
virtual void buildParts() = 0;
};
class HawaiianPizzaBuilder : public PizzaBuilder
{
public:
void buildParts() { pizza->setParts("cross + mild + ham&pineapple"); }
};
class SpicyPizzaBuilder : public PizzaBuilder
{
public:
void buildParts()
{
pizza->setParts("pan baked + hot + pepperoni&salami");
}
};
class Waiter
{
private:
PizzaBuilder *pizzaBuilder;
public:
void setPizzaBuilder(PizzaBuilder *pizzaBuilder)
{ /* Set the builder */
this->pizzaBuilder = pizzaBuilder;
}
Pizza *getPizza() { return pizzaBuilder->getPizza(); }
void construct()
{
pizzaBuilder->createNewPizza();
pizzaBuilder->buildParts();
}
};
int main()
{
Waiter *waiter = new Waiter();
PizzaBuilder *hawaiian_pizzabuilder = new HawaiianPizzaBuilder();
waiter->setPizzaBuilder(hawaiian_pizzabuilder);
waiter->construct();
cout << "pizza: " << waiter->getPizza()->getParts() << endl;
}
// Output:
// pizza: cross + mild + ham*pineapple
|
Example 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
| import java.util.*;
public class Main{
public static void main(String[] args) {
Director d = new Director();
Builder b1 = new Builder1();
d.Construct(b1);
Product p1 = b1.getResult();
p1.show();
}
}
class Product{
List<String> parts = new ArrayList<String>();
public void Add(String part){
parts.add(part);
}
public void show(){
System.out.println("The information of product:");
for(String s : parts)
System.out.print(s + " ");
System.out.println();
}
}
abstract class Builder{
public abstract void BuildPart();
public abstract Product getResult();
}
class Builder1 extends Builder{
Product p = new Product();
@Override
public void BuildPart(){
p.Add("A1");
p.Add("A2");
p.Add("C");
}
@Override
public Product getResult(){
return p;
}
}
class Builder2 extends Builder{
Product p = new Product();
@Override
public void BuildPart(){
p.Add("B1");
p.Add("B2");
p.Add("C");
}
@Override
public Product getResult(){
return p;
}
}
class Director{
public void Construct(Builder builder){
builder.BuildPart();
}
}
|