What is the Builder Pattern and How to use it in PHP?

As a result, we will create a new object.

That is only partially initialized and once again, the compiler will not detect any problem with it.

That’s the reason why Builder Pattern was born to save the world.

SolutionThe Builder Pattern is a creational pattern that solves the problem of initializing complex objects by providing a way to construct objects step by step and provide a method to returns the final complete object.

With the problem set, we will proceed to build the Builder class containing all the attributes of class BankAccount.

Besides, we will only use this Builder class to initialize a new object instead of using the constructor of class BankAccount like before.

Apply the Pattern Builder to solve our problem, here is all that we will get:public interface Builder{ public function setAccountNumber($accountNumber); public function setOwner($owner); public function setBranch($branch); public function setBalance($balance); public function setInterestRate($interestRate); public function build();}public class BankAccountBuilder implements Builder{ private $accountNumber; private $owner; private $branch; private $balance; private $interestRate; public function setAccountNumber($accountNumber) { $this->accountNumber = $accountNumber; return $this; } public function setOwner($owner) { $this->owner = $owner; return $this; } public function setBranch($branch) { $this->branch = $branch; return $this; } public function setBalance($balance) { $this->balance = $balance; return $this; } public function setInterestRate($interestRate) { $this->interestRate = $interestRate; return $this; } public function build() { return new BankAccount($this->accountNumber, $this->owner, $this->branch, $this->balance, $this->interestRate); }}public class BankAccount{ private $accountNumber; private $owner; private $branch; private $balance; private $interestRate; public function __construct($accountNumber, $owner, $branch, $balance, $interestRate) { $this->accountNumber = $accountNumber; $this->owner = $owner; $this->branch = $branch; $this->balance = $balance; $this->interestRate = $interestRate; } public function getAccountNumber() { return $this->accountNumber; } public function getOwner() { return $this->owner; } public function getBranch() { return $this->branch; } public function getBalance() { return $this->balance; } public function getInterestRate() { return $this->interestRate; } public function showInfo() { echo "XYZ Bank – Account Information"; echo "!."; if (!empty( (string) $this->getAccountNumber() )) echo "* Number: " .

$this->getAccountNumber(); if (!empty( (string) $this->getOwner() )) echo "* Owner: " .

$this->getOwner(); if (!empty( (string) $this->getBranch() )) echo "* Branch: " .

$this->getBranch(); if (!empty( (string) $this->getBalance() )) echo "* Balance: " .

$this->getBalance(); if (!empty( (string) $this->getInterestRate() )) echo "* Interest Rate: " .

$this->getInterestRate(); }}$bankAccount = new BankAccountBuilder().

setOwner("Homer").

setBalance(100.

00).

setInterestRate(2.

5).

build();$bankAccount.

showInfo();That’s it.We have completed setting up the Builder Pattern to solve our problem.

Let’s execute the code below to see the final result:XYZ Bank – Account Information;* Number: 789* Owner: Homer* Balance: 100.

00* Interest Rate: 2.

5By using the Builder Pattern, we can see that creating a new object has now become much easier and more intuitive than before.

It’s pretty awesome, right?The Pros and Cons of the Builder PatternProsThe Builder Pattern helps users avoid cramming their class with dozens of constructors.

The Builder Pattern helps you construct objects step-by-step, defer construction steps or run steps recursively.

With the Builder Pattern, users don’t need to pass null values to parameters that the object doesn’t need anymore.

The Builder Pattern helps your object creational code less error-prone as users will know what they are passing because of explicit method call.

With Builder Pattern, we can better control the building process of the object: we can add some code to validate the object before it is created and returned to the client.

The Builder Pattern helps your code become much easier to read and maintain in the future.

By using the Builder Pattern, you can isolate complex construction code from the business logic of the product.

ConsThe code may become much more complicated since you will need to introduce a lot of new subclasses to implement the pattern.

ConclusionIn this article, we have learned a lot about the Builder Pattern.

I hope that this design pattern will be a perfect solution to one of your software development problems sometime in the future.

In the next article of this series, we will learn about another creational design pattern called Prototype Pattern.

Stay tuned!.

. More details

Leave a Reply