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