04 Dec 2023



Intermediate

The Builder design pattern is a creational pattern that separates the construction of a complex object from its representation, allowing the same construction process to create different representations. An analogy for the Builder design pattern can be drawn from the construction of a house.

Imagine you want to build a house, and you have several options for the design and features. Instead of having a single constructor that takes all possible parameters for every type of house, you can use the Builder pattern.

In this analogy:

  1. Director (Architect): The director is like an architect who knows how to build different types of houses. The architect provides a set of blueprints (an interface) that specifies the steps involved in constructing a house. This interface may include methods for building walls, adding windows, installing doors, etc.

  2. Builder (Construction Crew): The builder is like a construction crew responsible for building the house according to the specifications provided by the architect. There can be different builders for different types of houses (e.g., a builder for modern houses, another for Victorian houses). Each builder knows how to build a specific type of house and implements the interface defined by the architect.

  3. Product (House): The product is the final constructed object, which in this case, is the house. The house can have various features like the number of floors, type of roof, interior design, etc. These features are set by the builder during the construction process.

  4. Client (Homeowner): The client is like the person who wants to build a house. The client interacts with the director (architect) to specify the type of house they want. The director, in turn, uses the appropriate builder to construct the desired house.

Here's a simplified example in code:

using System;

// Director
class Architect
{
    public void ConstructHouse(HouseBuilder builder)
    {
        builder.BuildWalls();
        builder.AddWindows();
        builder.InstallDoors();
        // Add more construction steps...
    }
}

// Builder Interface
abstract class HouseBuilder
{
    public abstract void BuildWalls();
    public abstract void AddWindows();
    public abstract void InstallDoors();
    // Add more construction methods...
}

// Concrete Builder
class ModernHouseBuilder : HouseBuilder
{
    public override void BuildWalls()
    {
        Console.WriteLine("Building modern walls");
    }

    public override void AddWindows()
    {
        Console.WriteLine("Adding modern windows");
    }

    public override void InstallDoors()
    {
        Console.WriteLine("Installing modern doors");
    }
    // Implement more construction methods for a modern house...
}

// Client
class Homeowner
{
    public void BuildHouse(Architect architect, HouseBuilder builder)
    {
        architect.ConstructHouse(builder);
    }
}

// Usage
class Program
{
    static void Main()
    {
        Architect architect = new Architect();
        ModernHouseBuilder modernBuilder = new ModernHouseBuilder();
        Homeowner homeowner = new Homeowner();
        homeowner.BuildHouse(architect, modernBuilder);

        // Keep console window open in debug mode
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

In this analogy, the Architect (Director) knows how to build a house, and the HouseBuilder (Builder) implements the construction process. The ModernHouseBuilder (Concrete Builder) is a specific builder for modern houses. The Homeowner (Client) interacts with the Architect to build the desired type of house using the appropriate builder.

software-design-patterns
builder-design-pattern
analogy