Understanding the Builder Pattern with PHP

The Builder Pattern is a creational design pattern that helps construct complex objects step by step. Unlike other creational patterns, the Builder pattern does not require the objects to be immutable and thus allows step-by-step configuration of the object before its final creation. This is particularly useful when an object requires several parameters for its creation, and those parameters need to be set in a specific order.

In this article, we’ll explore the Builder Pattern through a simple example in PHP where we build a Phone object with various attributes like operating system (OS), RAM, processor, screen size, and battery capacity.

  1. Index.php
<?php

use App\PhoneBuilder;

require "vendor/autoload.php";

//Builder pattern builds a complex object using simple objects and uses step by step approach.
//A Builder class builds the final object step by step. This builder is independent of other objects.

// We don't worry about the order of the parameters in the constructor.
$phone = new PhoneBuilder();
echo $phone->setBattery('5000')->setOs('ios')->setRam(3)->setProcessor('snapdragon')->getPhone();

In this file, we use the PhoneBuilder class to create a Phone object. The PhoneBuilder class allows us to set various properties of the Phone step-by-step and then retrieve the final Phone object using the getPhone method.

2. PhoneBuilder.php

<?php

namespace App;

class PhoneBuilder
{
    private $os = 'android';
    private $ram = 3;
    private $processor = 'snapdragon';
    private $screenSize = 5.8;
    private $battery = 4000;

    public function setOs($os)
    {
        $this->os = $os;
        return $this;
    }

    public function setRam($ram)
    {
        $this->ram = $ram;
        return $this;
    }

    public function setProcessor($processor)
    {
        $this->processor = $processor;
        return $this;
    }

    public function setScreenSize($screenSize)
    {
        $this->screenSize = $screenSize;
        return $this;
    }

    public function setBattery($battery)
    {
        $this->battery = $battery;
        return $this;
    }

    public function getPhone(): Phone
    {
        return new Phone($this->os, $this->ram, $this->processor, $this->screenSize, $this->battery);
    }
}

The PhoneBuilder class provides methods to set each attribute of the Phone object. Each setter method returns the builder instance ($this), allowing for method chaining. The getPhone method constructs and returns the final Phone object with the set attributes.

3. Phone.php

<?php

namespace App;

class Phone
{
    private $os;
    private $ram;
    private $processor;
    private $screenSize;
    private $battery;

    public function __construct($os, $ram, $processor, $screenSize, $battery)
    {
        $this->os = $os;
        $this->ram = $ram;
        $this->processor = $processor;
        $this->screenSize = $screenSize;
        $this->battery = $battery;
    }

    public function __toString()
    {
        return "OS: {$this->os}, RAM: {$this->ram}, PROCESSOR: {$this->processor}, SCREEN SIZE: {$this->screenSize}, BATTERY: {$this->battery}.\n";
    }
}

The Phone class defines the structure of the Phone object. The constructor initializes the phone’s attributes, and the __toString method provides a string representation of the Phone object, which we use to print the object’s details.

How It Works:

  1. Initialization: We create an instance of the PhoneBuilder class.
  2. Setting Attributes: Using the builder instance, we set the desired attributes of the phone. Each setter method returns the builder instance, allowing us to chain multiple setter calls.
  3. Building the Object: Once all the desired attributes are set, we call the getPhone method on the builder instance. This method creates and returns a Phone object with the specified attributes.
  4. Output: Finally, we print the Phone object, which triggers the __toString method of the Phone class, displaying the phone’s attributes.

Benefits of the Builder Pattern

  • Step-by-Step Construction: The Builder pattern allows for step-by-step construction of complex objects.
  • Method Chaining: It supports method chaining, making the code more readable and concise.
  • Independent Builders: Each builder is independent and can construct objects in different ways.
  • Flexible Object Creation: You can create different representations of an object using the same builder class.

The Builder Pattern is a powerful design pattern that simplifies the construction of complex objects. It provides a clear and flexible approach to setting multiple properties, making your code more maintainable and understandable.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top