Builder Design Pattern in C# with Example

1. Definition

The Builder pattern is a design pattern that allows you to construct complex objects step by step. It separates the construction of a complex object from its representation, ensuring a unique and specific configuration of the object.

2. Problem Statement

When you need to construct a "Computer" object, you know that computers have multiple components such as CPU, RAM, HDD, etc. Given the variety of computer configurations, how can you have an efficient and clear way to create different types of computers without cluttering the creation logic?

3. Solution

The Builder pattern lets you delegate the construction of the "Computer" object to a distinct builder class. The client then merely specifies the type and content of the object and retrieves the product from the builder.

4. Real-World Use Cases

1. Constructing complex meal orders in a restaurant system.

2. Creating characters in video games with numerous attributes and configurations.

3. Tailoring a set of financial reports for specific business scenarios.

5. Implementation Steps

1. Declare a builder interface with methods for creating parts of a product.

2. Implement the specific builders that adhere to the builder interface for each type of product.

3. Define a director class that will use the builder object to cohesively construct the product.

4. Use a client (in this case, our Main method) to orchestrate the building process.

6. Implementation in C#

// Builder Interface
public interface IComputerBuilder
{
    void AddCPU(string cpu);
    void AddRAM(string ram);
    void AddHDD(string hdd);
    Computer GetComputer();
}

// Concrete Builder
public class ComputerBuilder : IComputerBuilder
{
    private Computer _computer = new Computer();

    public void AddCPU(string cpu)
    {
        _computer.CPU = cpu;
    }

    public void AddRAM(string ram)
    {
        _computer.RAM = ram;
    }

    public void AddHDD(string hdd)
    {
        _computer.HDD = hdd;
    }

    public Computer GetComputer()
    {
        return _computer;
    }
}

// Product
public class Computer
{
    public string CPU { get; set; }
    public string RAM { get; set; }
    public string HDD { get; set; }

    public override string ToString()
    {
        return $"Computer Config:: CPU: {CPU}, RAM: {RAM}, HDD: {HDD}";
    }
}

// Director
public class ComputerDirector
{
    public Computer Construct(IComputerBuilder builder)
    {
        builder.AddCPU("Intel i7");
        builder.AddRAM("16GB DDR4");
        builder.AddHDD("1TB SSD");
        return builder.GetComputer();
    }
}

public class Program
{
    public static void Main()
    {
        IComputerBuilder builder = new ComputerBuilder();
        ComputerDirector director = new ComputerDirector();

        Computer myComputer = director.Construct(builder);
        Console.WriteLine(myComputer.ToString());
    }
}

Output:

Computer Config:: CPU: Intel i7, RAM: 16GB DDR4, HDD: 1TB SSD

Explanation:

The example demonstrates how a computer can be built using the Builder pattern in C#. 

The client (Main method) doesn't create the computer directly. Instead, the client specifies its configuration to a director class, which uses a builder to construct the product.

7. When to use?

Use the Builder pattern when:

1. The object needs to be constructed with numerous configurations.

2. The construction process of an object should be separate from its representation.

3. You want to have a clearer and more cohesive construction process.


Comments