ПІДТРИМАЙ УКРАЇНУ ПІДТРИМАТИ АРМІЮ
Uk Uk

Design Patterns in PHP 8: Builder

Design Patterns in PHP 8: Builder

For creating complex objects In the realm of software development use Design Patterns Builder

In the realm of software development, creating complex objects often feels like trying to solve a Rubik's cube. It's a step-by-step process, where every move is crucial and affects the final outcome. But what if we had a guide, a blueprint that could help us solve this puzzle effortlessly? In the world of programming, the Builder pattern serves as this guide.

The Builder pattern, a member of the esteemed creational design patterns family, is like a master craftsman in the world of object-oriented programming. It knows the intricacies of creating multifaceted objects, handling the construction process with finesse and precision. The beauty of the Builder pattern lies in its ability to construct diverse representations of an object, all while keeping the construction logic shielded from the client code.

In this article, we'll embark on a journey to explore the Builder pattern, unraveling its capabilities with PHP 8. We'll delve into its structure, understand its nuances, and see it in action with real-world examples. Whether you're building an intricate e-commerce platform or a simple blog, the Builder pattern can be your secret weapon to handle complex object creation with ease and elegance.

Imagine you're building an e-commerce application where you have a Product class. The Product class has many details like id, name, price, description, manufacturer, inventory, discount, etc.

Creating a Product object is a multi-step and sequential process. If we create multiple constructors for each attribute, it will lead to a large number of constructor parameters. This is known as the telescoping constructor anti-pattern.

class Product
{
 public int $id;
 public string $name;
 public int $price;
 public string $description;
 public string $manufacturer;
 public string $inventory;
 public int $discount;

 public function __construct(
 int $id,
 string $name,
 int $price,
 string $description,
 string $manufacturer,
 string $inventory,
 int $discount
 )
 {
 $this->id = $id;
 $this->name = $name;
 $this->price = $price;
 $this->description = $description;
 $this->manufacturer = $manufacturer;
 $this->inventory = $inventory;
 $this->discount = $discount;
 }
}

The Builder pattern can solve this problem.

class Product
{
 private $id;
 private $name;
 private $price;
 private $description;
 private $manufacturer;
 private $inventory;
 private $discount;

 public function __construct(ProductBuilder $builder)
 {
 $this->id = $builder->getId();
 $this->name = $builder->getName();
 $this->price = $builder->getPrice();
 $this->description = $builder->getDescription();
 $this->manufacturer = $builder->getManufacturer();
 $this->inventory = $builder->getInventory();
 $this->discount = $builder->getDiscount();
 }

 // getters for product details will go here
}

class ProductBuilder
{
 private $id;
 private $name;
 private $price;
 private $description;
 private $manufacturer;
 private $inventory;
 private $discount;

 public function setId($id)
 {
 $this->id = $id;
 return $this;
 }

 public function setName($name)
 {
 $this->name = $name;
 return $this;
 }

 public function setPrice($price)
 {
 $this->price = $price;
 return $this;
 }

 public function setDescription($description)
 {
 $this->description = $description;
 return $this;
 }

 public function setManufacturer($manufacturer)
 {
 $this->manufacturer = $manufacturer;
 return $this;
 }

 public function setInventory($inventory)
 {
 $this->inventory = $inventory;
 return $this;
 }

 public function setDiscount($discount)
 {
 $this->discount = $discount;
 return $this;
 }

 // getters will go here

 public function build()
 {
 return new Product($this);
 }
}

In this example, the ProductBuilder class is responsible for step-by-step construction of the Product object. The Product class itself has a constructor with a ProductBuilder argument. This way, we ensure that a product object is always created in a complete state.

$productBuilder = new ProductBuilder();
$product = $productBuilder
 ->setId(101)
 ->setName('iPhone 13')
 ->setPrice(999.99)
 ->setDescription('New iPhone 13 with A15 Bionic chip')
 ->setManufacturer('Apple Inc.')
 ->setInventory(1000)
 ->setDiscount(10)
 ->build();

As we reach the end of our exploration of the Builder pattern, it's clear to see why it's considered a cornerstone in the world of object-oriented programming. The Builder pattern elegantly solves the problem of constructing complex objects, reducing the intricacy and enhancing the readability of our code.

One of the standout features of the Builder pattern is its ability to separate the construction and representation of an object. This separation not only makes our code more modular but also improves its maintainability and scalability. It's like having a personal architect who understands our vision and brings it to life, one brick (or in our case, one object) at a time.

On a personal note, the Builder pattern holds a special place in my toolbox of design patterns. Over the years, it has proven to be an invaluable ally, helping me construct complex objects in my projects with ease and precision. Its versatility and robustness make it my go-to pattern when dealing with intricate object creation. It's like a trusted friend who never lets me down, no matter how complex the task at hand.

Ресурс : dev.to


Scroll to Top