Builder Design Pattern

Introduction
Hello all, today I back with another Design Patterns topic, and today I will share what i learned about Builder design pattern. I hope with this article you will know more about Builder design pattern and use it on your project.
What It Is?
The Builder design pattern is a creational pattern that provides a way to construct complex objects step by step. Builder design pattern allows us to reuse the same construction steps to build different types of the object, It decouples the object construction logic from the object representation.
When To Use It?
Here are some scenarios where implementing the Builder design pattern can be beneficial to a project:
When constructing an object requires multiple steps, configurations, or optional parameters, making direct construction complex and inefficient.
When improving code readability is necessary, as the pattern decouples object construction logic from its representation.
When there is a need to constructing immutable objects that require all attributes to be set during their initialization.
When there is a need to constructing different variations of an object using the same construction process.
Why Implement It?
Here are some pros and cons to consider before implementing the Builder design pattern in a project:
Pros
Makes complex object construction easier to understand.
Ensures consistent object construction across the application.
Decouples the construction logic from the business logic of the object.
Makes it easy to add new attributes or configurations without modifying existing code.
Cons
Increases code complexity by introducing additional classes and methods.
Might cause minor performance issues when used to create simple objects.
Can lead to tight coupling between the pattern and the objects it constructs.
How To Implement It?
Here are the steps to implement the Builder design pattern in a project:
Define the product class that needs to be constructed.
Define an interface or abstract class with methods to build the parts of the product.
Implement the builder interface or abstract class into concrete builder class with methods and construct the product steps by steps.
Create a director class isolates the construction process from the client.
In the client code, instantiate the director to construct the product and retrieve the final result.
Structure

Pseudocode
BEGIN CLASS Product
PUBLIC FIELD partA
PUBLIC FIELD partB
PUBLIC FIELD partC
BEGIN PUBLIC CONSTRUCTOR Product(partA, partB, partC)
this.partA = partA
this.partB = partB
this.partC = partC
END CONSTRUCTOR
END CLASS
BEGIN INTERFACE IBuilder
PUBLIC PROCEDURE setPartA()
PUBLIC PROCEDURE setPartB()
PUBLIC PROCEDURE setPartC()
PUBLIC FUNCTION build(): Product
END INTERFACE
BEGIN CLASS ConcreteBuilder IMPLEMENTS IBuilder
PRIVATE FIELD partA
PRIVATE FIELD partB
PRIVATE FIELD partC
BEGIN PUBLIC PROCEDURE setPartA()
partA = "PartA"
END PROCEDURE
BEGIN PUBLIC PROCEDURE setPartB()
partB = "PartB"
END PROCEDURE
BEGIN PUBLIC PROCEDURE setPartC()
partC = "PartC"
END PROCEDURE
BEGIN PUBLIC FUNCTION build()
RETRUN new Product(partA, partB, partC)
END FUNCTION
END CLASS
BEGIN CLASS Director
PRIVATE FIELD builder: IBuilder
BEGIN PUBLIC CONSTRUCTOR Director(builder: IBuilder)
this.builder = builder
END CONSTRUCTOR
BEGIN PUBLIC PROCEDURE setBuilder(builder: IBuilder)
this.builder = builder
END PROCEDURE
BEGIN PUBLIC FUNCTION construct()
iBuilder.setPartA()
iBuilder.setPartB()
iBuilder.setPartC()
RETRUN iBuilder.build();
END FUNCTION
END CLASS
BEGIN CLASS Client
BEGIN PRIVATE PROCEDURE useBuilder()
FIELD builder = new ConcreteBuilder()
FIELD director = new Director(builder)
FIELD result = director.construct()
END PROCEDURE
END CLASS
Additional Information
I have discovered some additional information about Builder design pattern. If you have any additional information about Builder design pattern that you'd like to share, please feel free to leave a comment.
The Builder design pattern can be combined with Singleton Design Pattern to make the builder accessible throughout the system.
The Builder design pattern can be combined with the Fluent Interface design pattern to simplify the construction steps, making the code easier to read and maintain, especially when constructing complex objects.
The Builder design pattern is often used to construct objects of classes that have many constructor parameters (usually more than three). However, it’s a good idea to first check if the class can be simplified, as having too many parameters might indicate that the class is too complex or violates the Single Responsibility Principle (SRP).




