What was the problem in factory design pattern?
We create a class and assign all responsibilities for object creation. This class have static method also called static factory.
Remember we pass parameters to factory method and these parameters will be pass to the next class constructor, whose object we want to create.
What was the problem?
The parameters we want to pass in next class constructor, first they all need to pass to factory method.
The order of the parameters matters. Might be some parameters are optional. Therefore management of parameters is very difficult.
Imagine you are building a system for Car, Burger, Computer.
These system have
1. multiple parts.
2. can be created with different configuration
3. some parts are optional
4. You can't pass 10+ argument to construction order management start complex.
Without Builder pattern
Your constructor is become messy.
Disadvantages
Hard to read.
Error-prone.
Not Flexible(can't easily change the order and Skip the optional).
Using Builder pattern
Build object step-by-step.
Separate construction logic from object creation.
Multiple objects can be created using the same builder pattern.
Definition:
Builder design pattern is one of the creational design pattern that help you to build step by step creation of the object. Finally return object with desire attributes values.
Real Life Example Car building of Builder Design pattern.
Goal
we want to build a car with different parts like
Color,
Engine,
GPS,
Transmission,
Seats
Product Class: Car
2. Builder Interface
3. Concrete Builder
4. Director (Optional)
5. Client Code
🧙♂️ The Story of Building a Custom Sports Car
Once there is a factory name Brain motors which build cars, Now their is a demand of building of customized car. Customer want customized Sport Cars.
Now Every customer have different demand, some want auto engine, some want different seat color and some want GPS other not.
Factory workers become tired of answer the customer different demand.
Now factory manager decide to hire builder, which will build each part of the car step by step.
This code will always produce clean, customize cars.
🧱 The Builder: SportCarBuilder
meet the build, who build the car piece by piece.
🧑💼 The Director (CarDirector)
Here is the man who know, have data(information) how to build different models of the car.
He will provide instruction to the builder to build specific car model.
👨💻 The Client (You!)
Here the client came to the factory and say
"I want a Red Sports Car with leather seats, GPS, automatic transmission, and a V8 engine"
Manager Replies
"Let me call my director and builder and they will take care of it"
Now
You(Client) have now worry how the car is building.
The car is building in step-by-step order.
The director control the recipe.
The cars are perfectly customized every time.
Now the factory producing more car, with well structure code, the customers are also happy.
Real world application example
1. Ecommerce Project
In an ecommerce website, in creating order (check out) you have to get information like
1. Customer Info
2. Cart Item
3. Shipping Info
4. Discounts
5. Payment Method
6. Invoice
You don't want to constructor to get 10+ parameters.
You would do like that
2. Generating PDF or Document (Resume, Invoices, Repots)
Laravel use laravel dompdf , Snappy may internally use builder like logic.
3. Hotel Booking and Travel package
We have hotel management system where we have option to build custom travel packages.
1. Flight
2. Hotel
3. Cab
4. Activities
Now customer have many facility to get their desire custom package. No Need To Change The System.
4. Database Query Builder (Laravel)
In Laravel we have query builder to step by step build query, using builder method.
You are building the query step-by-step.
5. Notification System
Suppose you are building the notification, where you build notification like as
set title, set message, set receipt, set channel, set schedule
The code will be look alike
6. HTML or FORM Builder (Laravel's form facade)
In Laravel you can build form adding each method on the builder class, On each method call form state change.
Comments
Post a Comment