C # Design Pattern consolidation Notes-builder mode

Source: Internet
Author: User

Objective

Write to yourself-your insistence. The builder model is not complicated, but it's hard to think of an image.

Introduction-builder Mode

Definition: Separates the construction of a complex object from its representation so that the same build process can create different representations.

Realize

The builder model is mostly about dealing with complex objects, and the previous example of a skateboard shoe doesn't look right because it's not complicated. But we split a pair of shoes: soles, laces, uppers and so on. It is also not simple, the following through the builder model to achieve a shoe-making process:

The first is the builder class:

    /// <summary>    ///Builders ' base class/// </summary>     Public Abstract class Builder {/// <summary>        ///assembling the soles/// </summary>         Public Abstract voidBuildtread (); /// <summary>        ///assembling the upper/// </summary>         Public Abstract voidBuildvamp (); /// <summary>        ///Shoe Straps/// </summary>         Public Abstract voidBuildshoelace (); /// <summary>        ///Shoes Factory/// </summary>        /// <returns></returns>         Public Abstract Shoes outshoes (); }

Who is the builder in the factory, of course the worker:

 // <summary>    ///operator 1/// </summary>     Public class Operator1 : Builder { Shoes Shoes=New Shoes();  Public Override voidbuildshoelace () {shoes. Addpart ("shoelace 1"); }         Public Override voidBuildtread () {shoes. Addpart ("Sole 1"); }         Public Override voidBuildvamp () {shoes. Addpart ("Upper 1"); }         Public Override Shoes outshoes () {returnshoes; }    }        /// <summary>    ///operator 2/// </summary>     Public class Operator2 : Builder { Shoes Shoes=New Shoes();  Public Override voidbuildshoelace () {shoes. Addpart ("shoelace 2"); }         Public Override voidBuildtread () {shoes. Addpart ("Sole 2"); }         Public Override voidBuildvamp () {shoes. Addpart ("Upper 2"); }         Public Override Shoes outshoes () {returnshoes; }    }

or the product-shoes:

 // <summary>    ///Shoes/// </summary>     Public class Shoes { PublicShoes () {Parts=New List<string>(); } List <string> Parts {Get;Set; }  Public voidAddpart (stringPart )        {Parts.add (part); }         Public voidShowparts () { for(inti =0, C = Parts.count;i < C; i++) {Console.WriteLine ("The order in which my skateboard shoes are built is _{0}:{1}", I,parts[i]); }        }    }

If it were your workers, would you work consciously? (I won't, hehe ...) So need the boss to command):

    Public class   Boss    {        public   Shoes buidshoes (builder Builder)        {            Builder. Buildtread ();            Builder. Buildvamp ();            Builder. Buildshoelace ();             return Builder. Outshoes ();        }    }

Here's the call:

namespacedesignpattern{classProgram {Static voidMain (string[] args) {Boss Boss=NewBoss (); Builder Builder1=NewOperator1 (); Shoes shoes1=boss.            Buidshoes (Builder1); Shoes1.            Showparts (); Console.WriteLine ("-----------------Gorgeous split-line-----------------"); Builder Builder2=NewOperator2 (); Shoes Shoes2=boss.            Buidshoes (BUILDER2); Shoes2.            Showparts ();        Console.read (); }    }}

Operation Result:

Welcome to criticize correct, reprint please indicate source http://www.cnblogs.com/xinwang/p/6380564.html

C # Design Pattern consolidation Notes-builder mode

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.