Design Pattern (9) --- builder Pattern

Source: Internet
Author: User

I. Definition

Builder mode: separates the construction of a complex object from its representation, so that different representations can be created during the same build process.

Explanation: In the builder mode, you only need to specify the types to be constructed to obtain them, but you do not need to know the specific construction process and details.

 

Ii. UML class diagram

III. Basic Code

class Program    {        static void Main(string[] args)        {            Director director = new Director();            Builder builder1 = new ConcreteBuilder1();            director.Construct(builder1);            Product product = builder1.GetProduct();            product.Show();            Console.Read();        }    }    class Product    {        IList<string> parts = new List<string>();        public void Add(string part)        {            parts.Add(part);        }        public void Show()        {            Console.WriteLine("\n product create");            foreach (string part in parts)            {                Console.WriteLine(part);            }        }    }    abstract class Builder    {        public abstract void BuildPartA();        public abstract void BuildPartB();        public abstract Product GetProduct();    }    class ConcreteBuilder1 : Builder    {        private Product product = new Product();        public override void BuildPartA()        {            product.Add("part A");        }        public override void BuildPartB()        {            product.Add("part B");        }        public override Product GetProduct()        {            return product;        }    }    class Director    {        public void Construct(Builder builder)        {            builder.BuildPartA();            builder.BuildPartB();        }    }

In the basic code, the ctor conductor directs the builder to create the product. All specific builders inherit the interface class builder to create a specific product. In the client code, the conductor and the specific Builder are instantiated, and the conductor directs the specific builder to create the product to complete the functions of the production product.

 

Iv. Specific instances

Specific requirements: the buyer (client) needs to purchase a batch of computers. He will go to the computer city to find the boss (conductor) instructions, and the boss will arrange personnel to assemble the computers according to the customer's requirements. Throughout the entire process, the buyer will not involve complex engineering of computer assembly. He is only responsible for raising demands and then purchasing the computer.

Product computers:

Public class computer {private ilist <string> parts = new list <string> (); Public void add (string part) {parts. add (part);} public void show () {console. writeline ("computer assembly starts ................. "); foreach (string part in parts) {console. writeline ("component" + part + "installed");} console. writeline ("Computer assembled ");}}

 

Abstract builder:

public abstract class Builder    {        public abstract void BuildPartCPU();        public abstract void BuildPartMainBoard();        public abstract Computer GetComputer();    }

 

Specific builder:

public class ConcreteBuilder1 : Builder    {        Computer computer = new Computer();        public override void BuildPartCPU()        {            computer.Add("CPU");        }        public override void BuildPartMainBoard()        {            computer.Add("main board");        }        public override Computer GetComputer()        {            return computer;        }    }

 

Conductor boss:

public class Director    {        public void Construct(Builder builder)        {            builder.BuildPartCPU();            builder.BuildPartMainBoard();        }    }

 

Client:

Director director = new Director();            Builder builder1 = new ConcreteBuilder1();            director.Construct(builder1);            Computer computer1 = builder1.GetComputer();            computer1.Show();

 

V. simplified example:

In a specific application, either the conductor or the abstract builder can be omitted. In this example, only the product class and builder are allowed:

Product Type:

Public class product {private ilist <string> parts = new list <string> (); Public void add (string part) {parts. add (part);} public void show () {console. writeline ("product assembly starts ................. "); foreach (string part in parts) {console. writeline ("component" + part + "installed");} console. writeline ("Product Assembled ");}}
View code

 

Builder:

public class Builder    {        private Product product = new Product();        public void BuildPartA()        {            product.Add("PartA");        }        public void BuildPartB()        {            product.Add("PartB");        }        public Product GetProduct()        {            return product;        }        public void Construct()        {            BuildPartA();            BuildPartB();        }    }
View code

 

Client:

builder = new Builder();            builder.Construct();            Product product = builder.GetProduct();            product.Show();

 

Vi. Summary

Steps for implementing the builder mode: 1) Compiling the product class; 2) Compiling the abstract builder class (which can be omitted); 3) Compiling the specific builder; 4) Compiling the conductor class command builder how to assemble the product; finally, the client can call it. The builder mode decouples the assembly process and specific components by separating the build code from the presentation code. In addition, the internal details of how to assemble the client are hidden, so that the client does not have to worry about how to assemble, just need to use.

Design Pattern (9) --- builder Pattern

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.