Use Builder mode to build a car, and builder mode to build a car

Source: Internet
Author: User

Use Builder mode to build a car, and builder mode to build a car

 

Builder mode can also be used for car creation.


For the Builder model, the prime minister wants to 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()
        {
            Console.WriteLine(Model);
            Console.WriteLine(Engine);
            Console.WriteLine(Body);
Console. WriteLine ("number of Doors:" + Doors );
            Console.WriteLine(Transmission);
Console. WriteLine ("configuration :");
            foreach (var accessory in Accessories)
            {
                Console.WriteLine("\t{0}", accessory);
            }
        }
    }

 

This Car can be handed over to Builder1 or Builder2, but first an abstract class of the Builder needs to be extracted. This abstract Builder can set and obtain the 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 specific Builder is required 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 = "variable speed 1 ";
        }
        public override void SetBody()
        {
            _car.Body = "SUV";
        }
        public override void SetAccessories()
        {
_ Car. Accessories. Add ("Interior ");
_ Car. Accessories. Add ("Exterior Decoration ");
        }
        public override void SetDoors()
        {
            _car.Doors = 6;
        }
    }   

 

There may be many derived classes in this abstract Builder. Therefore, you need a class to manage these derived Builder to decide which Builder to choose for production.

 

   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, find the Builder management class and pass in the specific Builder through its constructor.

 

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

 

Abstract Factory and Builder models can both build cars, but the two may be different:

 

● The abstract factory model is equivalent to finding an exclusive factory for a car, requiring the factory to only create this type of car
● The Builder mode is equivalent to finding different factories for vehicles. Different factories produce different models.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.