Tag:建造者模式
Category:设计模式
Article From:https://www.cnblogs.com/zkhbalance/p/9125170.html

Constructor model of design pattern (three)

  Today, to learn the builder pattern, it seems that there is also someone called generator.

  A concept

  Builder pattern: using multiple simple objects to build a complex object step by step. This type of design pattern belongs to the creation mode, which provides the best way to create objects. A Builder class constructs the final object step by step. The Builder class isIndependent of other objects.

  Two introduction

  Intention:takeA complex construction is separated from its representation so that the same construction process can be made.To create different representations.

  The main solution:The main solution is in software systems, sometimes faced with the creation of “a complex object”, which is usually made up of a certain algorithm by the sub objects of each part; the various parts of the complex object often face dramatic changes due to changes in the requirements, but the algorithms that combine them are relatively stable..

  When to use:Some basic components do not change, and their combinations often change.

  How to solve it:Divide the change from the same.

  Key code:Builder: create and provide examples, director: manage the dependencies of the built instances.

  Application examples: 1、To go to KFC, hamburgers, coke, French fries, fried chicken wings and so on are invariable, and their combinations are constantly changing, forming the so-called “set meal”. 2, StringBuilder in JAVA.

  Advantage: 1、The builders are independent and easy to expand. 2, it is easy to control the details of risk.

  Shortcomings: 1、The product must have a common point, and the scope is limited. 2, if internal changes are complex, there will be many construction classes.

  Use the scene: 1、Objects that need to be generated have complex internal structures. 2, the internal properties of the generated objects themselves depend on each other.

  Matters needing attention:The difference from the factory pattern is that the builder mode pays more attention to the sequence of parts assembly.

  Three illustrations

  Three examples of living examples

  Let me give an example of life.

  When it comes to the builder mode, I want to buy computers. This example is more appropriate.

  If you want to buy some computer accessories to assemble themselves, the computer has a lot of accessories, such as CPU, motherboard, chassis and so on… But you do not understand the computer accessories and assembly, then you come to me, because you think I am a programmer, I understand, I do know better than you, but I’m lazy again.So, I gave a specification, looking for a running leg to help me buy and assemble it according to this specification… It’s a little messy, no matter, then stroking it, you’re the same as the director (director). I’m the abstract Builder (Builder) that you’re looking for.The concrete Builder (ConcreteBuilder), the computer is the final product.

  The picture on the top is copy people, smooth out the relationship, the next way I want to write the code name may be different from the picture, but it doesn’t matter, the code can be read.

  Let’s offer a picture again.

 

  Four code display

 /// <summary>
    /// main program/// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Builder builder = new ConcreBuilder();
            Director director = new Director(builder);
            Computer computer = director.CreateComputer();
            Console.WriteLine("The computer has been created");
            Console.WriteLine(computer.ToString());
            Console.ReadKey();
        }
    }
========================
 /// <summary>
    /// Director/// </summary>
    class Director
    {
        Builder builder;
        public Director(Builder b) {
            this.builder = b;
        }
        public Computer CreateComputer() {
            builder.BuildPart();
            return builder.GetComputer();
        }
    }
========================
/// <summary>
    /// Abstract builder/// </summary>
    interface Builder
    {
        //The way to create a product
        void BuildPart();
        //Get the created product
        Computer GetComputer();
    }
========================
/// <summary>
    /// ConcreteBuilder/// </summary>
    class ConcreBuilder : Builder
    {
        Computer computer = new Computer();
        public void BuildPart()
        {
            computer.BuildCpu();
            computer.BuildJiXiang();
            computer.BuildZhuBan();
        }

        public Computer GetComputer()
        {
            return computer;
        }
    }
========================
/// <summary>
    /// Computer products/// </summary>
    class Computer
    {
        public string CPU { get; set; }
        public string ZhuBan { get; set; }
        public string JiXiang { get; set; }

        public void BuildCpu() {
            Console.WriteLine("CPU is being created...");
            this.CPU = "intel core i7";
            Console.WriteLine("CPUCreate and complete...");
        }
        public void BuildZhuBan()
        {
            Console.WriteLine("The main board is being created...");
            this.ZhuBan = "Asus motherboard";
            Console.WriteLine("The motherboard is created...");
        }
        public void BuildJiXiang()
        {
            Console.WriteLine("The chassis is being created...");
            this.JiXiang = "Handmade custom chassis";
            Console.WriteLine("The chassis creation is completed...");
        }
        public override string ToString()
        {
            return "Show the computer configuration:"+this.CPU+"  "+this.ZhuBan+ "  " + this.JiXiang;
        }
       
    }

View Code

The code has been prepared for you. Is it very intimate?

 This is the result of the program.

 

Warm tip: StringBuilder in.Net is the builder pattern used.

Leave a Reply

Your email address will not be published. Required fields are marked *