DEV Community

Juarez Júnior
Juarez Júnior

Posted on

C# Design Pattern: Builder

The Builder pattern is used to construct complex objects step by step in a more organized way. In this example, the Builder is adjusted to allow method chaining, making the code more fluent and easier to read. This is useful in situations where the object has multiple configurations or optional parts, such as building a house or creating a customized order. Using a Builder simplifies the process and reduces the likelihood of errors.

C# Code Example:

// Product to be built
public class House
{
    public string Foundation { get; set; }
    public string Structure { get; set; }
    public string Roof { get; set; }
    public string Interior { get; set; }

    public void ShowDetails()
    {
        Console.WriteLine($"House with Foundation: {Foundation}, Structure: {Structure}, Roof: {Roof}, Interior: {Interior}");
    }
}

// Builder interface
public interface IHouseBuilder
{
    IHouseBuilder BuildFoundation();
    IHouseBuilder BuildStructure();
    IHouseBuilder BuildRoof();
    IHouseBuilder BuildInterior();
    House GetHouse();
}

// Concrete Builder implementation
public class HouseBuilder : IHouseBuilder
{
    private House _house = new House();

    public IHouseBuilder BuildFoundation()
    {
        _house.Foundation = "Concrete foundation";
        return this;
    }

    public IHouseBuilder BuildStructure()
    {
        _house.Structure = "Wooden structure";
        return this;
    }

    public IHouseBuilder BuildRoof()
    {
        _house.Roof = "Tile roof";
        return this;
    }

    public IHouseBuilder BuildInterior()
    {
        _house.Interior = "Standard interior";
        return this;
    }

    public House GetHouse()
    {
        return _house;
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Using the builder with method chaining
        IHouseBuilder builder = new HouseBuilder();
        House house = builder.BuildFoundation()
                             .BuildStructure()
                             .BuildRoof()
                             .BuildInterior()
                             .GetHouse();

        house.ShowDetails();  // Output: House with Foundation: Concrete foundation, Structure: Wooden structure, Roof: Tile roof, Interior: Standard interior
    }
}
Enter fullscreen mode Exit fullscreen mode

Code Explanation:

In this example, the builder was modified to return the current instance (this) from each method, allowing the house to be built with method chaining. This style of coding makes the construction process clearer and more intuitive. Finally, the GetHouse method returns the constructed house.

Conclusion:

The Builder pattern is a great solution for creating complex objects in a controlled and readable way. By allowing method chaining, the construction process becomes more intuitive and organized, making it ideal for scenarios where multiple configurations or options need to be adjusted.

Source code: GitHub

Top comments (0)