3. Builder in PHP

Tram Ho

Hello anhem, it’s been a long time since I came back to the Desgin Pattern with PHP series. The other day, I introduced Abstract Factory , a relatively famous design pattern. Today I will continue to introduce another design pattern, which is the Builder design pattern.

The article continues to relate to Dota 2 as I mentioned from the previous article to make the readings confusing and difficult to understand for people who do not know how to play games. So learn how to play Dota 2 to better understand your hand.

1. Intro

The previous section I have introduced that we have all 23 design patterns and are divided into 3 main branches. The design design pattern is in the Creational group, which is primarily intended to help us build object creation, which helps increase flexibility and reuse.

In the sense of this pattern it allows us to gradually build complex objects. It allows us to create objects of different types or different representations with the same code structure. Oke sounds quite complicated, we’ll try to find out how it works

2. The problem

Imagine the system we want to create an object as a Hero. It is not difficult to create this object, as the previous sections we introduced, it is relatively easy to build a hero, but the producers they want to make money from the product. They require equipment such as clothes, canvas, effects for each hero.

If the heroes are the same in shape (limbs face), oke okay. But the structure of each hero is different, the hero has a body with 2 arms, has 2 legs, a head with hair (like an invoker) … and a beast hero has a hammer handle, has 4 legs, the head has horn (con centau type) … Therefore, installing each component 1 will be very difficult, we will example one of the following cases:

Okay, so I passed it on to turn into a game character of that shape and there are variables I haven’t used. We need a way to build a Hero in a more general and easier way like that.

3. Solution

The design pattern builder separates multiple steps in a complex object – called Product into a set of operations to initialize each part of the object. A Director class decides whether the object will have an initialization order, linking those components. The last class is the Builder class that will actually build the components for that class.

This architecture is built as follows (this is cop from this page)

Architecture Builder pattern

We see :

  • Builder: provides an interface for the Director to create components of a complex object called Product. From these components will build on the Product.
  • ConcreteBuilder: Install detailed content of each component corresponding to the object you want to initialize. Each of these guys corresponds to 1 Product.
  • Director: The player will call the Director to create the Product from the components provided by the Builder.

This theory is too long for me to implement it quickly (I wrote in the early morning so I was a bit sad)

They find that after creating, with each type of Hero Builder, we can make it different Head, Body, … components while maintaining the format of a hero that must have 3 parts. Hero Hero is a complex object because it requires the ability to change the shape of each hero (each hero has different shapes) but in summary, there is still a general structure of three main parts.

The code is relatively long, so you can map it with the diagram I drew above, you will find it really relatively simple.

4. Fasteners

Builder Design Pattern is one of the important patterns that helps build a complex object, used in many frameworks today (typical you see in PHP is QueryBuilder in laravel).

Of course, you do not have to follow 100% of the patterns as shown, you can apply and combine with a few other design patterns to assist in creating a builder for your object.

Thank you for reading the article, if there is anything wrong or difficult to understand, please comment below to discuss.

Reference material

Share the news now

Source : Viblo