使用Builder模式造车

时间:2023-06-10 12:25:44

Builder模式也可以造车。

对于Builder模式来说,首相要把要造的车确定下来:

    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()
        {
            Console.WriteLine(Model);
            Console.WriteLine(Engine);
            Console.WriteLine(Body);
            Console.WriteLine("门的数量:" + Doors);
            Console.WriteLine(Transmission);
            Console.WriteLine("配置为:");
            foreach (var accessory in Accessories)
            {
                Console.WriteLine("\t{0}", accessory);
            }
        }
    }


这个Car可以交给Builder1也可以交给Builder2,等等,但首先需要提炼出Builder们的一个抽象类。这个抽象Builder可以设置和获取Car,并提供生产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();
    }


然后,就需要一个具体的Builder来派生抽象Builder。

  public class BuilderOne : CarBuilder
    {
        public override void SetEngine()
        {
            _car.Engine = "发动机1";
        }

        public override void SetModel()
        {
            _car.Model = "型号1";
        }

        public override void SetTransmission()
        {
            _car.Transmission = "变速1";
        }

        public override void SetBody()
        {
            _car.Body = "SUV";
        }

        public override void SetAccessories()
        {
            _car.Accessories.Add("内饰");
            _car.Accessories.Add("外饰");
        }

        public override void SetDoors()
        {
            _car.Doors = 6;
        }
    }


这个抽象Builder的派生类可能有很多,所以需要一个管理这些派生Builder的类,用来决定到底选用哪个Builder来生产。

   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();
        }
    }


在客户端,如果需要造车,就找Builder的管理类,通过它的构造函数传入具体的Builder。

    class Program
    {
        static void Main(string[] args)
        {
            var builderManager = new BuilderManager(new BuilderOne());
            builderManager.BuildCar();
            var car = builderManager.GetCar();
            car.ShowCarInfo();
            Console.ReadKey();
        }
    }

抽象工厂和Builder模式都可以造车,但两者有也许的不同:

● 抽象工厂模式相当于为车找了一个专属工厂,要求工厂只能造这种类型的车
● Builder模式相当于为车找不同的工厂,不同的工厂生产出来的车型都不一样