net Design Patterns (Builder Patterns) Learning Notes

Keywords: Mobile

The use of design patterns is only to solve a class of problems. When solving the current class of problems, it usually brings other problems when solving this problem.

net Design Patterns (Builder Patterns) Learning Notes

  • Summary: Compared with the previous engineering model, the builder model encapsulates the construction process more than once in the construction process, so as to deal with the complex problems in the construction process.

 

Let's split up a car builder, for example. Building cars is more complex than previous examples. So we added a layer in the construction process, a layer that the factory model does not have, to control the complex problems in the real construction process!

 

    //Abstract Automobile Workshop
    public abstract class AbstractBuilder
    {
        public string _Body{ get; set; }
        public string _Light { get; set; }
        public string _Wheel { get; set; }
        //Making headlights
        public abstract void Light();
        //Manufacturing steering wheel
        public abstract void Wheel();
        //Manufacturing body
        public abstract void Body();

        public abstract Car ResoultCar();
    }



    class AudiBuilder : AbstractBuilder
    {
        public override void Body()
        {
            Console.WriteLine("Car maker");
            this._Body = "Audi body";
        }

        public override void Light()
        {
            Console.WriteLine("Car lamp");
            this._Light = "Audi List";
        }

        public override void Wheel()
        {
            Console.WriteLine("Car steering wheel");
            this._Wheel = "Audi Wheel";
        }

        public override Car ResoultCar()
        {
            return new Car(this._Body, this._Light, this._Wheel);
        }
    }
    ///Mercedes-Benz Factory
    class BenzBuilder : AbstractBuilder
    {
        public override void Body()
        {
            Console.WriteLine("Car maker");
            this._Body = "Benz Body";
        }

        public override void Light()
        {
            Console.WriteLine("Car lamp");
            this._Light = "Benz Light";
        }

        public override void Wheel()
        {
            Console.WriteLine("Car steering wheel");
            this._Wheel = "Benz wheel";
        }

        public override Car ResoultCar()
        {
           return  new Car(this._Body,this._Light,this._Wheel);
        }
    }

    class BYDBuilder : AbstractBuilder
    {
        public override void Body()
        {
            Console.WriteLine("Car maker");
        }

        public override void Light()
        {
            Console.WriteLine("Car lamp");
        }

        public override void Wheel()
        {
            Console.WriteLine("Car steering wheel");
        }

        public override Car ResoultCar()
        {
            return new Car(this._Body, this._Light, this._Wheel);
        }
    }

    public class Car
    {
        public Car(string wheel, string body, string light)
        {
            Console.WriteLine( "car:"+wheel+body+light);
        }
    }

    //Automobile Company Management
    public class DirecterSkill
    {
        private AbstractBuilder builder;
        public DirecterSkill(AbstractBuilder builder)
        {
            this.builder = builder;
            builder.Body();
            builder.Wheel();
            builder.Light();
        }
   

        public void Resoult()
        {
            this.builder.ResoultCar() ;
        }
    }


        static void Main(string[] args)
        {
            AbstractBuilder builder     = new BYDBuilder();
            DirecterSkill directerSkill = new DirecterSkill(builder);
            directerSkill.Resoult ();


            Console.ReadKey();
        }

 

In this way, the construction of this model is one more layer of processing in the construction process than the abstract factory.

Posted by n00b Saibot on Thu, 24 Jan 2019 09:27:14 -0800