On this page
4. Builder
The Builder pattern is a design pattern that separates the construction of a complex object from its representation. This allows the same construction process to create different representations of an object. It’s particularly useful when an object needs to be created step-by-step, and the final representation of the object may vary.
Key Characteristics:
- Step-by-Step Construction: Constructs a complex object step-by-step.
- Flexible Representation: Allows the same construction process to produce different representations.
- Encapsulation: Encapsulates the construction logic and allows different parts of an object to be constructed independently.
How It Works:
- Builder Interface: Defines methods for creating the parts of the complex object.
- Concrete Builder: Implements the builder interface to construct and assemble the parts of the product.
- Product: Represents the complex object that is being built.
- Director: Constructs the object using a builder instance, directing the construction process.
Example in PHP:
// Product
class Product {
private $parts = [];
public function addPart($part) {
$this->parts[] = $part;
}
public function showParts() {
return implode(', ', $this->parts);
}
}
// Builder Interface
interface Builder {
public function buildPartA();
public function buildPartB();
public function getResult(): Product;
}
// Concrete Builder
class ConcreteBuilder implements Builder {
private $product;
public function __construct() {
$this->product = new Product();
}
public function buildPartA() {
$this->product->addPart("PartA");
}
public function buildPartB() {
$this->product->addPart("PartB");
}
public function getResult(): Product {
return $this->product;
}
}
// Director
class Director {
private $builder;
public function __construct(Builder $builder) {
$this->builder = $builder;
}
public function construct() {
$this->builder->buildPartA();
$this->builder->buildPartB();
}
}
// Usage
$builder = new ConcreteBuilder();
$director = new Director($builder);
$director->construct();
$product = $builder->getResult();
echo $product->showParts(); // Output: PartA, PartB
Explanation:
- Product: The
Product
class represents the complex object that is being built. It has methods for adding parts and displaying them. - Builder Interface: The
Builder
interface defines methods for creating different parts of the product. - Concrete Builder: The
ConcreteBuilder
class implements theBuilder
interface. It constructs the product and assembles its parts. - Director: The
Director
class uses aBuilder
instance to construct the product. It controls the construction process by calling builder methods in a specific sequence.
Benefits:
- Separation of Concerns: Separates the construction process from the representation of the product.
- Flexibility: Allows different representations of the product to be created using the same construction process.
- Encapsulation: Encapsulates the complex construction logic, making it easier to maintain and extend.
Use Cases:
- Complex Object Construction: When an object needs to be created with multiple parts and configurations.
- Fluent Interfaces: When creating objects with a fluent interface where different parts can be set independently.
The Builder pattern is particularly useful when dealing with complex object creation, ensuring that the construction process is flexible and maintainable.