SoFunction
Updated on 2025-04-11

C# builds cars through Builder mode

Builder mode can also make cars.

For the Builder model, the Prime Minister must determine the car to be built:

    public class Car
    {
        public string Model { get; set; }
        public string Engine { get; set; }
        public string Transmission { get; set; }
        public string Body { get; set; }
        public int Doors { get; set; }
        public List<string>  Accessories { get; set; }
        public Car()
        {
            Accessories = new List<string>();
        }
        public void ShowCarInfo()
        {
            (Model);
            (Engine);
            (Body);
            ("Number of doors:" + Doors);
            (Transmission);
            ("Configuration as:");
            foreach (var accessory in Accessories)
            {
                ("\t{0}", accessory);
            }
        }
    }

This Car can be handed over to Builder1 or Builder2, etc., but first, you need to extract an abstract class of Builders. This abstract Builder can set and get a Car and provide an abstract method for producing each part of the Car.

   public abstract class CarBuilder
    {
        protected Car _car;
        public Car GetCar()
        {
            return _car;
        }
        public void SetCar()
        {
            _car = new Car();
        }
        public abstract void SetModel();
        public abstract void SetEngine();
        public abstract void SetTransmission();
        public abstract void SetBody();
        public abstract void SetDoors();
        public abstract void SetAccessories();
    }

Then, a concrete Builder is needed to derive the abstract Builder.

  public class BuilderOne : CarBuilder
    {
        public override void SetEngine()
        {
            _car.Engine = "Engine 1";
        }
        public override void SetModel()
        {
            _car.Model = "Model 1";
        }
        public override void SetTransmission()
        {
            _car.Transmission = "Speed ​​1";
        }
        public override void SetBody()
        {
            _car.Body = "SUV";
        }
        public override void SetAccessories()
        {
            _car.("Interior");
            _car.("Exterior decoration");
        }
        public override void SetDoors()
        {
            _car.Doors = 6;
        }
    }

There may be many derived classes for this abstract Builder, so a class that manages these derived Builders is needed to decide which Builder to use to produce.

   public class BuilderManager
    {
        private readonly CarBuilder _carBuilder;
        public BuilderManager(CarBuilder carBuilder)
        {
            _carBuilder = carBuilder;
        }
        public void BuildCar() 
        {
            _carBuilder.SetCar();
            _carBuilder.SetModel();
            _carBuilder.SetEngine();
            _carBuilder.SetBody();
            _carBuilder.SetDoors();
            _carBuilder.SetTransmission();
            _carBuilder.SetAccessories();
        }
        public Car GetCar()
        {
            return _carBuilder.GetCar();
        }
    }

On the client, if you need to build a car, look for the Builder management class and pass it into the specific Builder through its constructor.

    class Program
    {
        static void Main(string[] args)
        {
            var builderManager = new BuilderManager(new BuilderOne());
            ();
            var car = ();
            ();
            ();
        }
    }

Both the abstract factory and the Builder model can build cars, but there are possible differences between the two:

  • The abstract factory model is equivalent to finding an exclusive factory for the car, requiring the factory to only build this type of car.
  • The Builder model is equivalent to finding different factories for the car, and the models produced by different factories are different.

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the relevant links below