C # Design pattern: Builder pattern

Source: Internet
Author: User

One, builder pattern

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_4. Builder Mode {//Builder Pattern: separates the construction of a complex object from its representation so that the same build process can create different representations.    Builder mode is an object-creation pattern. //the difference from an abstract factory is that an abstract factory is targeted at a series of objects (such as a variety of vehicle objects), whereas the creator pattern is a local change for an object (such as a single object with different weapons, etc.)    classProgram {Static voidMain (string[] args) {Builder Builder=NewUSA ("Black Skin","American","English"); Director Director=NewDirector (builder); People product=Director.            Construct (); Console.Write ("\ r \ n"); Builder Builder1=NewUSA ("White Skin","British","Japanese Language"); Director Director1=NewDirector (Builder1); People product1=Director1.            Construct ();        Console.readkey (); }    }    /// <summary>    ///Creating abstract Classes/// </summary>     Public Abstract classBuilder {protectedPeople people =Newpeople ();  Public Abstract voidSetcountry ();  Public Abstract voidSetspeak ();  Public Abstract voidSetskincolor ();  PublicPeople GetResult () {returnpeople; }    }    /// <summary>    ///Objects that are created are different locally,/// </summary>     Public classpeople { Publicpeople () {Console.Write ("We're all human, but"); }    }     Public classUsa:builder {Private string_country; Private string_speak; Private string_skincolor;  PublicUSA (stringSkincolor,stringCountry,stringspeak) {             This. _country =Country;  This. _skincolor =Skincolor;  This. _speak =speak; }         Public Override voidSetskincolor () {Console.Write (_skincolor+"of the"); }         Public Override voidSetcountry () {Console.Write (_country); }         Public Override voidSetspeak () {Console.Write ("speak"+_speak); }    }     Public classUk:builder {Private string_country; Private string_speak; Private string_skincolor;  PublicUK (stringSkincolor,stringCountry,stringspeak) {             This. _country =Country;  This. _skincolor =Skincolor;  This. _speak =speak; }         Public Override voidSetskincolor () {Console.Write (_skincolor+"of the"); }         Public Override voidSetcountry () {Console.Write (_country); }         Public Override voidSetspeak () {Console.Write ("speak"+_speak); }    }    /// <summary>    ///performer, creating object/// </summary>     Public classDirector {PrivateBuilder Builder;  PublicDirector (Builder bu) { This. Builder =bu; }         PublicPeople Construct () {Builder.            Setskincolor (); Builder.            Setcountry (); Builder.            Setspeak (); returnBuilder.getresult (); }    }}

Two, according to the code above, we will feel like an abstract factory, what are they two?

1 "builder mode separates the construction of a complex object from its representation, allowing the same build process to create different representations. Builder mode is a complex object-creation pattern.

2 "Abstract Factory is for the series of objects, builder mode is a complex object

Third, how do you understand a complex object that is relatively abstract to the factory and builder?

We use people as an object to understand:

1 "In the builder model, we can create American and English objects through abstract classes, and this object has its own independent things, such as the black-skinned American English-speaking and white-skinned English speaking Japanese, people here understand to be the same complex object

2 "and in the heavy abstract factory, in the British factory, only white-skinned Britons speak Japanese. Without the advent of black-skinned Americans speaking English (an American-owned factory), so for abstract factories, at this point we understand the British and the Americans as two different objects, so the abstract factory is for the series of objects

C # Design pattern: 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.