Builder Design Pattern in .NET
The Builder pattern is used to construct a complex object step-by-step, allowing you to vary the representation of the object using the same construction process. It separates the construction logic from the actual object structure.
Purpose
To simplify the
construction of complex objects by separating the object building process
from the final representation. This is useful when an object has many optional
parts or configuration steps.
Key Characteristics
- Step-by-step
construction of an object
- A builder
class defines how each part is created
- The final
object is returned after full construction
- Supports method
chaining and fluent APIs
Pros
- Controls the
construction process clearly
- Improves code
readability using chaining (fluent API)
- Makes it
easier to construct different representations or configurations
Cons
- Can
introduce extra complexity with many classes
- Not ideal
for very simple objects
Use Cases
- Building custom
financial reports, invoices, or loan applications with many optional fields
- Constructing
complex payment requests to APIs
- Configuring notifications
or workflows with different steps and rules
Real-Time Finance Example: Building a Loan
Application
Suppose you're
creating a loan application system where an application contains many optional
parts like income, assets, liabilities, co-applicant, etc.
The Builder
pattern allows you to build these step-by-step and only include what's
necessary.
1. Define the Product
public class LoanApplication
{
public string ApplicantName { get; set; }
public decimal Income { get; set; }
public decimal LoanAmount { get; set; }
public string CoApplicantName { get; set; }
public bool HasExistingLoan { get; set; }
public void Show()
{
Console.WriteLine($"Applicant: {ApplicantName}");
Console.WriteLine($"Income: ₹{Income}");
Console.WriteLine($"Loan Amount: ₹{LoanAmount}");
Console.WriteLine($"Co-Applicant:
{CoApplicantName}");
Console.WriteLine($"Existing Loan: {(HasExistingLoan ?
"Yes" : "No")}");
}
}
2. Create the Builder Interface
public interface ILoanBuilder
{
ILoanBuilder SetApplicant(string name);
ILoanBuilder SetIncome(decimal income);
ILoanBuilder SetLoanAmount(decimal amount);
ILoanBuilder SetCoApplicant(string name);
ILoanBuilder SetExistingLoan(bool hasLoan);
LoanApplication Build();
}
3. Implement the Concrete Builder
public class HomeLoanBuilder : ILoanBuilder
{
private readonly LoanApplication _application = new();
public ILoanBuilder SetApplicant(string name)
{
_application.ApplicantName = name;
return this;
}
public ILoanBuilder SetIncome(decimal income)
{
_application.Income = income;
return this;
}
public ILoanBuilder SetLoanAmount(decimal amount)
{
_application.LoanAmount
= amount;
return this;
}
public ILoanBuilder SetCoApplicant(string name)
{
_application.CoApplicantName = name;
return this;
}
public ILoanBuilder SetExistingLoan(bool hasLoan)
{
_application.HasExistingLoan = hasLoan;
return this;
}
public LoanApplication Build()
{
return _application;
}
}
4. Client Code Usage
class Program
{
static void Main()
{
var builder = new HomeLoanBuilder();
LoanApplication application = builder
.SetApplicant("Priya Sharma")
.SetIncome(85000)
.SetLoanAmount(1500000)
.SetCoApplicant("Rajesh Sharma")
.SetExistingLoan(true)
.Build();
application.Show();
}
}
Output
Applicant: Priya Sharma
Income: ₹85000
Loan Amount: ₹1500000
Co-Applicant: Rajesh Sharma
Existing Loan: Yes
Summary
- Builder
Pattern is ideal
for constructing objects with many parts or configurations
- Keeps the
object creation readable and maintainable
- Great for finance
apps where workflows and inputs vary by user, context, or product
(loan, EMI, invoice)
Comments
Post a Comment