Script
Script
Builder Pattern
Explanation : By employing a builder method, we can avoid including all constructor parameters that
aren't always required to create an object. Rather, we permit this builder method to move the object
construction code to distinct objects known as builders and extract it from its own class. Object
construction is arranged by the pattern into a series of steps (buildWalls, buildDoor, etc.). You use a
builder object and a series of these steps to create an object.
A director is someone who has the authority to give orders and carry out the building steps, much like a
manager or OIC of a company. The director is in charge of all the builders that are a part of an object.
You can reuse different construction routines throughout your program by placing them in the director
class.
Furthermore, the client code is never made aware of the specifics of the product construction thanks to
the director class. All the client has to do is assign a builder to a director, work with the director to start
construction, and then receive the builder's output.
The goal of the builder pattern is to separate the construction of an object from its representation.
As you can see in this image, there is a class Item in this scenario of pizza hut wherein it has the name
function which is a string, size function which is also a string, and price function which is a float. Object’s
representation of the Item/Pizza/ColdDrink.
Methods to set this field – for example sa house, setStories, setdoortype, and setRooftype
Build:
Representation yung unang house, tapos yung ikalawa na house builder ay construction.
1. Builder Interface: Define an interface that specifies the steps involved in constructing the object.
Each step may correspond to setting a particular attribute or configuration option.
2. Concrete Builders: Implement concrete builder classes that implement the builder interface and
provide specific implementations for constructing the object. Each concrete builder is
responsible for constructing a particular type of object.
3. Product: Define the object being constructed. This is typically a complex object that requires
multiple steps to be fully initialized.
4. Director: In some implementations, a director class may be used to orchestrate the construction
process. The director class interacts with the builder to initiate the construction steps in a
particular order.
5. The Client must associate one of the builder objects with the director. Usually, it’s done just
once, via parameters of the director’s constructor. Then the director uses that builder object for
all further construction. However, there’s an alternative approach for when the client passes the
builder object to the production method of the director. In this case, you can use a different
builder each time you produce something with the director.
The first level contains the PizzaBuilder class, which is responsible for constructing pizzas.
The next level includes the methods of the PizzaBuilder class: addCrust(), addToppings(), setCheese(),
setSauce(), and getPizza().
Finally, the leaf nodes represent the attributes of the Pizza class: crust, toppings, cheese, and sauce.
This diagram visually illustrates how the components of the Pizza Hut operation are structured, with the
PizzaBuilder orchestrating the construction process and the Pizza class representing the final product.