Builder - Design Pattern

Builder - Design Pattern

Software Architecture Simplified

ยท

3 min read

Objective ๐ŸŽฏ

Simplify the construction of complex objects by creating it step by step.

Type โœ…

โŒBehavioral: Describes how objects interact/communicate between themselves.

โœ”๏ธCreational: Describes how to instantiate an object without large and complex.

โŒStructural: Describes how objects/classes are composed to form larger structures.

UML ๐Ÿ“

image.png

Participants ๐Ÿ”—

โ€ข Builder:

  • Specify an abstract interface for creating parts of a Product object

โ€ข ConcreteBuilder:

  • Constructs and assembles parts of the Product by implementing the Builder interface
  • Provides an interface for retrieving the product

โ€ข Director:

  • Constructs an object using the Builder interface

โ€ข Product:

  • Represents the complex object under construction
  • May include other classes to represent its parts

Sample Code ๐ŸŽฎ

Structural Example ๐Ÿ›๏ธ

image.png

public static class BuilderStructural
    {
        public static void Execute()
        {
            Director lDirector = new Director();

            // Using Builder 1
            Builder lConcreteBuilder1 = new ConcreteBuilder1(); // Define Builder
            lDirector.Construct(lConcreteBuilder1);             // Build Product
            Product lProduct1 = lConcreteBuilder1.GetResult();  // Get Product
            Console.WriteLine("Builder 1 - Product Result: ");
            lProduct1.Show();

            // Using Builder 2
            Builder lConcreteBuilder2 = new ConcreteBuilder2(); // Define Builder
            lDirector.Construct(lConcreteBuilder2);             // Build Product
            Product lProduct2 = lConcreteBuilder2.GetResult();  // Get Product
            Console.WriteLine("\n\rBuilder 2 - Product Result: ");
            lProduct2.Show();
        }
    }

    public class Director
    {
        public void Construct(Builder prBuilder)
        {
            // Builder can use a complex series of steps
            prBuilder.BuildPartOne();
            prBuilder.BuildPartTwo();
        }
    }

    public abstract class Builder
    {
        protected Product _Product = new Product();
        public abstract void BuildPartOne();
        public abstract void BuildPartTwo();
        public abstract Product GetResult();
    }

    public class ConcreteBuilder1 : Builder
    {
        public override void BuildPartOne()
        {
            _Product.Add("Part A");
        }

        public override void BuildPartTwo()
        {
            _Product.Add("Part B");
        }

        public override Product GetResult()
        {
            return _Product;
        }
    }

    public class ConcreteBuilder2 : Builder
    {
        public override void BuildPartOne()
        {
            _Product.Add("Part C");
        }

        public override void BuildPartTwo()
        {
            _Product.Add("Part D");
        }

        public override Product GetResult()
        {
            return _Product;
        }
    }

    public class Product
    {
        public List<string> _Parts = new List<string>();

        public void Add(string prPart)
        {
            _Parts.Add(prPart);
        }

        public void Show()
        {
            Console.WriteLine("Product created with the following parts: " + String.Join(", ", _Parts));
        }
    }

Output image.png

Real-world Example ๐Ÿ”ฅ

image.png

public static class BuilderPractical
    {
        public static void Execute()
        {
            VehicleManifacturer lVehicleManifacturer = new VehicleManifacturer();

            // Building MotorCycle
            VehicleBuilder lVehicleBuilder = new MotorCycleBuilder();
            lVehicleManifacturer.Construct(lVehicleBuilder);
            Vehicle lMotorCycle = lVehicleBuilder.GetVehicle();
            Console.WriteLine("MotorCycle Builder - Product Result: ");
            lMotorCycle.GetDetails();

            // Building Car
            lVehicleBuilder = new CarBuilder();
            lVehicleManifacturer.Construct(lVehicleBuilder);
            Vehicle lCar = lVehicleBuilder.GetVehicle();
            Console.WriteLine("\n\rCar Builder - Product Result: ");
            lCar.GetDetails();
        }
    }

    public class VehicleManifacturer
    {
        public void Construct(VehicleBuilder prVehicleBuilder)
        {
            prVehicleBuilder.BuildFrame();
            prVehicleBuilder.BuildEngine();
            prVehicleBuilder.BuildWheels();
            prVehicleBuilder.BuildDoors();
        }
    }

    public abstract class VehicleBuilder
    {
        protected Vehicle _Vehicle;
        public abstract void BuildFrame();
        public abstract void BuildEngine();
        public abstract void BuildWheels();
        public abstract void BuildDoors();

        public Vehicle GetVehicle()
        {
            return _Vehicle;
        }
    }

    public class MotorCycleBuilder : VehicleBuilder
    {
        public MotorCycleBuilder()
        {
            _Vehicle = new Vehicle("MotorCycle");
        }

        public override void BuildFrame()
        {
            _Vehicle.Frame = "MotorCycle Frame";
        }

        public override void BuildEngine()
        {
            _Vehicle.Engine = "500 cc";
        }

        public override void BuildWheels()
        {
            _Vehicle.Wheels = 2;
        }

        public override void BuildDoors()
        {
            _Vehicle.Doors = 0;
        }
    }

    public class CarBuilder : VehicleBuilder
    {
        public CarBuilder()
        {
            _Vehicle = new Vehicle("Car");
        }

        public override void BuildFrame()
        {
            _Vehicle.Frame = "Car Frame";
        }

        public override void BuildEngine()
        {
            _Vehicle.Engine = "2500 cc";
        }

        public override void BuildWheels()
        {
            _Vehicle.Wheels = 4;
        }

        public override void BuildDoors()
        {
            _Vehicle.Doors = 4;
        }
    }

    public class Vehicle
    {
        public string VehicleType { get; set; }
        public string Frame { get; set; }
        public string Engine { get; set; }
        public int Wheels { get; set; }
        public int Doors { get; set; }

        public Vehicle(string prVehicleType)
        {
            VehicleType = prVehicleType;
        }

        public void GetDetails()
        {
            Console.WriteLine($"Engine: {Engine} | Frame: {Frame} | Wheels: {Wheels} | Doors: {Doors}");
        }
    }

Output

image.png

Source Code ๐ŸŽฒ

github.com/VictorLins/DesignPatterns

Did you find this article valuable?

Support Victor Lins by becoming a sponsor. Any amount is appreciated!

ย