. NET builder Model explained

Source: Internet
Author: User
Definition of builder Mode:

Separates the construction of a complex object from its representation so that the same build process can create different representations, such that the design pattern is called the builder pattern

Builder mode structure diagram:

Builder Mode role:

1 Builder: Specify an abstract interface for each part that creates a product object.
2 ConcreteBuilder: Implements the builder interface to construct and assemble the individual parts of the product, defines and clarifies the representations it creates, and provides an interface for retrieving the product.
3 Director: Constructs an object that uses the builder interface.
4 Product: Represents the complex object being constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process, which contains the classes that define the constituent parts, including the interfaces that assemble the parts into the final product.

The following is an example of a real-life building that interprets the builder model:

1. Abstract the builder interface, which is to be implemented in order to create a house type of conditions, after the creation of the number of returned rooms, as well as the description of the house information.

<summary>///Abstract Builder///</summary>public interface ihouse{///<summary>///conditions for the creation of a house type//</ summary>///<returns></returns>bool Getbackyard (); Number of rooms created by <summary>/////</summary>///<returns></returns>long noofrooms (); <summary>///description///</summary>///<returns></returns>string Description ();}

2. Inherit Ihouse interface, concrete builder, here create a room, which includes a living room, kitchen, toilet, bedroom, a total of four rooms such a house.

public class Croom{public string Roomname {get; set}}//<summary>///concrete Builder///</summary>public class CSFH : ihouse{private bool Mblnbackyard;private Hashtable Rooms; public csfh () {croom = new Croom (); Roomname = "First floor living room"; Rooms = new Hashtable (); Rooms.add ("Room1", "Guest"); The new Croom (); Roomname = "Kitchen on the first floor"; Rooms.add ("Room2", "Guest"); The new Croom (); Roomname = "Toilet on the first floor"; Rooms.add ("Room3", "Guest"); The new Croom (); Roomname = "First floor bedroom"; Rooms.add ("Room4", "Guest"); Mblnbackyard = true;} public bool Getbackyard () {return mblnbackyard,} public long Noofrooms () {return rooms.count;} public string Description ()  {IDictionaryEnumerator myenumerator = Rooms.getenumerator (); string strdescription = "This House altogether" + Rooms.count + "between \ n"; while (Myenumerator.movenext ()) {strdescription = strdescription + "\ n" + Myenumerator.key + "\ T" + ((Croom) myenumerator.value). Roomname;} return strdescription;}}

3. Inherit Ihouse interface, concrete builder, here create a house, which only includes bedroom, living room, kitchen a total of three rooms such a house.

<summary>///Other Concrete Builders///</summary>public class capt:ihouse{private bool Mblnbackyard;private Hashtable Rooms; Public CApt () {Rooms = new Hashtable (); Croom = new Croom (); Roomname = "bedroom"; Rooms.add ("Room1", "Guest"); The new Croom (); Roomname = "living room"; Rooms.add ("Room2", "Guest"); The new Croom (); Roomname = "Kitchen"; Rooms.add ("Room3", "Guest"); Mblnbackyard = false;} public bool Getbackyard () {return mblnbackyard,} public long Noofrooms () {return rooms.count;} public string Description () {IDictionaryEnumerator myenumerator = Rooms.getenumerator (); string strdescription = "This House altogether" + Rooms.count + "room \ n"; whil E (Myenumerator.movenext ()) {strdescription = strdescription + "\ n" + Myenumerator.key + "\ T" + ((Croom) Myenumerator.value ). Roomname;} return strdescription;}}

4. Create a mentor who directs which builders to build what kind of room.

<summary>///mentor///</summary>public class Cdirector{public IHouse buildhouse (bool blnbackyard) {if ( Blnbackyard) {return new CSFH ();} Else{return new CApt ();}}}

5. Create:

static void Main (string[] args) {cdirector objdirector = new Cdirector ();//instantiation guide ihouse objhouse;string Input = console.re Adline ();//The input condition directs which creator creates the room Objhouse = Objdirector.buildhouse (bool. Parse (Input)); Console.WriteLine (Objhouse.description ()); Console.ReadLine ();}

The builder pattern is used primarily to "build a complex object in steps" where "step-by" is a stable algorithm, and the parts of complex objects change frequently

The product does not require abstract classes, especially because of the complexity of the algorithm that created the object, or when this pattern is applied to the production process of the product, the end result may vary greatly and it is unlikely that an abstract product class will be extracted.

The previous abstract factory pattern solves the demand change of "series objects", and the Builder mode solves the requirement change of "object part".

The use of the builder model allows the internal representation of the product to change independently. Using the builder mode allows the client to not know the details of the internal composition of the product

Each builder is relatively independent and not related to the other builder.

The builder pattern is applicable to the dependency of the properties of the product objects that need to be generated, and the builder pattern can force the build order. The product objects that need to be generated have complex internal structures.

  • 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.