In software development, sometimes we need to create complex objects that contain multiple fields and configuration options. Even if we try to build such objects using a constructor, we may end up having too many parameters, which obviously makes the code hard to read and difficult to maintain.
This is where Builder Design Pattern becomes useful.
Builder Pattern is a creational design pattern that helps to construct the complex objects step by step. It separates the object construction process from its representation and allows the same construction process to create different representations.
The Problem: Creating Complex Objects
Imagine we are building an HR management system. While working on the Add Employee feature we need to create an employee object. An employee might need the following information: FirstName, LastName, Email, Department, Position, Salary, Health Insurance, Pension Plan etc. If we use a constructor, the code might become like this:
public Employee(string firstName,
string lastName,
string email,
string department,
string position,
decimal salary,
bool hasHealthInsurance,
bool hasPensionPlan)
{
this.FirstName = firstName;
// Other initializations
}Now, creating an employee object look like this:
var employee = new Employee(
"Nabaraj",
"Ghimire",
"[email protected]",
"IT",
"Senior Developer",
100000,
true,
true
);Problems with this approach
Too many parameters
Hard to remember parameter order
Poor readability
Difficult to extend later
Components and Step by Step implementation
Product: The object being created
Builder: Defines steps to build the object
Concrete Builder: Implements the building steps
Director (optional): Controls the building process
Client: Uses the builder
Step 1: Product Class
Let’s define an Employee class.
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Department { get; set; }
public string Position { get; set; }
public decimal Salary { get; set; }
public bool HasHealthInsurance { get; set; }
public bool HasPensionPlan { get; set; }
}Step 2: Builder Interface
Let’s define the steps required to build an Employee.
public interface IEmployeeBuilder
{
IEmployeeBuilder SetFirstName(string firstName);
IEmployeeBuilder SetLastName(string lastName);
IEmployeeBuilder SetEmail(string email);
IEmployeeBuilder SetDepartment(string department);
IEmployeeBuilder SetPosition(string position);
IEmployeeBuilder SetSalary(decimal salary);
IEmployeeBuilder AddHealthInsurance();
IEmployeeBuilder AddPensionPlan();
Employee Build();
}Step 3: Concrete Builder
Let’s implement the Builder.
public class EmployeeBuilder : IEmployeeBuilder
{
private Employee _employee = new Employee();
public IEmployeeBuilder SetFirstName(string firstName)
{
_employee.FirstName = firstName;
return this;
}
public IEmployeeBuilder SetLastName(string lastName)
{
_employee.LastName = lastName;
return this;
}
public IEmployeeBuilder SetEmail(string email)
{
_employee.Email = email;
return this;
}
public IEmployeeBuilder SetDepartment(string department)
{
_employee.Department = department;
return this;
}
public IEmployeeBuilder SetPosition(string position)
{
_employee.Position = position;
return this;
}
public IEmployeeBuilder SetSalary(decimal salary)
{
_employee.Salary = salary;
return this;
}
public IEmployeeBuilder AddHealthInsurance()
{
_employee.HasHealthInsurance = true;
return this;
}
public IEmployeeBuilder AddPensionPlan()
{
_employee.HasPensionPlan = true;
return this;
}
public Employee Build()
{
return _employee;
}
}Step 4: Client Code
Now we can create Employee like this:

Join the conversation! Your thoughts help the community grow.