C # design Pattern-builder mode

Source: Internet
Author: User

First, the Builder model introduction:

Builder pattern: Separates the construction of a complex object from its representation so that the same build process can create different representations, separating the construction code from the presentation code, so that the client does not have to know the details of the internal composition of the product, This reduces the degree of coupling between the client and the specific product. The essence of the builder model is to decouple the assembly process from the creation of the specific product.

Second, scenario examples:

or notebook as an example, the computer store want to enter a batch of notebooks, the buyer to the Notebook factory pickup, notebook has many accessories such as CPU, memory, motherboard, power supply and so on, it is impossible to let the buyer go to their own an accessory to install, the factory is in advance to let the workers installed a batch of notebooks. This allows the builder model to be implemented, encapsulating the assembly of each accessory in the notebook into a builder class, and the builder simply returns to the finished product object.

Third, the relevant code:

1, create the product class, including the collection of product accessories:

    /// <summary>    ///Product (Notebook) class/// </summary>     Public classNoteBook {//collection of notebook components        Privateilist<string> parts =Newlist<string>(); //Add a single component to the collection of notebook components         Public voidADD (stringPart ) {Parts.        ADD (part); }        //show the installation process         Public voidShow () {Console.WriteLine ("The notebook is being assembled ...."); foreach(varIteminchparts) {Console.WriteLine ("Components"+item+"It's all packed! "); } Console.WriteLine ("Notebook assembly Complete! "); }    }

2. Create the builder abstract parent class, because the installation process of the notebook accessory is consistent:

    /// <summary>    ///Builder Abstract Parent class/// </summary>     Public Abstract classBuilder {//Install CPU         Public Abstract voidbuildpartcpu (); //Installed Memory         Public Abstract voidbuildpartmemory (); //get the assembled notebooks         Public AbstractNoteBook Getnotebook (); }

3. Create a leader in the builder model, directing concrete builders to assemble notebooks:

    /// <summary>    /// Conductor Class     /// </summary>     Public class Commander    {        // Assemble notebooks         Public void Construct (builder builder)        {            Builder. Buildpartcpu ();            Builder. Buildpartmemory ();        }    }

4, the creation of concrete builders, rewrite the abstract assembly process method, the difference lies in its assembly of different accessories:

    /// <summary>    ///Concrete Builder Class/// </summary>     Public classWorker1:builder {NoteBook NoteBook=NewNoteBook ();  Public Override voidbuildpartcpu () {notebook. ADD ("i5-cpu"); }         Public Override voidbuildpartmemory () {notebook. ADD ("4g-kingston"); }         Public OverrideNoteBook Getnotebook () {returnnotebook; }    }
    /// <summary>    ///specific manufacturer class/// </summary>     Public classWorker2:builder {NoteBook NoteBook=NewNoteBook ();  Public Override voidbuildpartcpu () {notebook. ADD ("i7-cpu"); }         Public Override voidbuildpartmemory () {notebook. ADD ("8g-kingston"); }         Public OverrideNoteBook Getnotebook () {returnnotebook; }    }

5. Call

Commander Commander =NewCommander (); //worker XiaomingBuilder xiaoming =NewWorker1 (); //The conductor called Xiaoming to assemble the notebook.commander.            Construct (xiaoming); //Xiao Ming has a notebook installedNoteBook Notebook1 =xiaoming.            Getnotebook (); Notebook1.            Show (); //worker Xiao HongBuilder Xiaohong =NewWorker2 (); //The conductor called Little Red to assemble the notebook.commander.            Construct (Xiaohong); //Little fine feminine attire, good notebook.NoteBook Notebook2 =Xiaohong.            Getnotebook (); Notebook2.            Show (); Console.readkey ();

Iv. Summary:

Key to the implementation of the construction model:

    1. In the builder mode, the conductor deals directly with the client, and the conductor divides the client's request to create the product into a build request for each part, which is then delegated to the specific builder role, and the concrete builder's role is to complete the construction of the specific product, but it is not known to the customer.
    2. The builder pattern is primarily used to "step-through-build a complex object", where "step-by" is a fixed combination process, and the parts of the complex object are constantly changing, which means that the internal components of the notebook are constantly changing, such as the performance of the hard disk and CPU products.
    3. The product does not require abstract classes, as the final product created by the build pattern may vary greatly, so it is unlikely that an abstract product class will be extracted.
    4. The abstract factory model solves the demand change of the "series", while the builder model solves the need for "product parts".
    5. Since the builder hides the assembly process for a specific product, it is necessary to change the internal representation of a product by implementing a specific builder, which will be able to respond well to changes in the requirements of the constituent components of the product.

C # design Pattern-builder mode

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.