Skip to content

Design Patterns: Builder

Published: at 01:14 PM

The Builder Design Pattern is a creational design pattern that focuses on the construction process of complex objects. It allows for the separation of the construction logic from the representation of the object, making it possible to create different representations using the same construction process.

Understanding the Builder Pattern

The core idea behind the Builder Pattern is captured by the following definition:

“Separates the construction of a complex object from its representation so that the same construction process can create different representations.”

— Design Patterns: Elements of Reusable Object-Oriented Software

In simpler terms, the Builder Pattern:

Diagram

The following diagram illustrates the key components of the Builder Pattern:

builder
Director
+Construct()
Builder
+BuildPart()
ConcreteBuilder
+BuildPart()
+GetResult()
Product

Components Explained

Key Points

Practical Example

Consider a car dealership needing to generate various sales reports. Without using the Builder Pattern, a SalesReport class might look like this:

public class SalesReport
{
    public string HeaderSection;
    public string SalesSection;
    public string OfficeBreakdown;

    public string Debug()
    {
        return new StringBuilder()
            .AppendLine(HeaderSection)
            .AppendLine(SalesSection)
            .AppendLine(OfficeBreakdown)
            .ToString();
    }
}

To implement the Builder Pattern, first define a builder interface:

public interface ISalesReportBuilder
{
    void AddHeader();
    void AddSales();
    void AddOfficeBreakdown();

    SalesReport GetSalesReport();
}

This interface outlines the construction process but leaves the implementation details to concrete builder classes. The purpose here is to keep the interface generic, so it can be used for different types of reports (e.g., weekly, monthly, quarterly).

Next, implement the concrete builder class:

public class DailySalesReportBuilder : ISalesReportBuilder
{
    private SalesReport _report;

    public DailySalesReportBuilder()
    {
        Reset();
    }

    public void Reset()
    {
        _report = new SalesReport();
    }

    public void AddHeader()
    {
        _report.HeaderSection = "----------------------SALES REPORT-----------------";
    }

    public void AddSales()
    {
        _report.SalesSection = "Sales Data:\nTotal sales: ${amount}";
    }

    public void AddOfficeBreakdown()
    {
        _report.OfficeBreakdown = "Office Breakdown Data";
    }

    public SalesReport GetSalesReport()
    {
        SalesReport finishedReport = _report;
        Reset();
        return finishedReport;
    }
}

Key Implementation Details

Conclusion

The Builder Design Pattern is a powerful tool for managing the creation of complex objects, particularly when different representations of the object are required. By abstracting the construction logic and separating it from the object’s representation, the Builder Pattern provides flexibility and control, making it easier to handle complex initialization scenarios.

Understanding and applying the Builder Pattern can greatly enhance the maintainability and scalability of your code, especially in situations where complex object creation and configuration are involved.