. NET builder mode and. net construction

Source: Internet
Author: User

. NET builder mode and. net construction

Builder mode definition:

Separates the construction of a complex object from its representation so that different representations can be created during the same construction process. Such a design pattern is called the builder pattern.

Builder mode structure diagram:

Builder mode role:

1 builder: specify an abstract interface for each part of a product object.
2 ConcreteBuilder: implements the Builder interface to construct and assemble each part of the product, define and define the representation it creates, and provide an interface for retrieving the product.
3 Director: Construct an object using the Builder interface.
4. Product: a complex object to be constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process, including the class that defines the components, including the interfaces that assemble these components into the final product.

The following describes how to build a house in real life:

1. Abstract The Builder interface, including the conditions for creating the house type to be implemented, the number of rooms returned after the creation, and the description of the house.

/// <Summary> /// abstract builder /// </summary> public interface IHouse {// <summary> /// conditions for creating a house type /// </summary> // <returns> </returns> bool GetBackyard (); /// <summary> /// Number of created rooms /// </summary> /// <returns> </returns> long NoOfRooms (); /// <summary >/// Description /// </summary> /// <returns> </returns> string Description ();}

2. inherit from the IHouse interface. The builder has created a room including the living room, kitchen, bathroom, bedroom, and four rooms.

Public class CRoom {public string RoomName {get; set ;}/// <summary> /// specific builder /// </summary> public class CSFH: IHouse {private bool extends nbackyard; private Hashtable Rooms; public CSFH () {CRoom room = new CRoom (); room. roomName = "first floor living room"; Rooms = new Hashtable (); Rooms. add ("room1", room); room = new CRoom (); room. roomName = "kitchen on the first floor"; Rooms. add ("room2", room); room = new CRoom (); room. roomName = "restroom on the first floor"; Rooms. add ("room3", room); room = new CRoom (); room. roomName = "first floor bedroom"; Rooms. add ("room4", room); required nbackyard = true;} public bool GetBackyard () {return required nbackyard;} public long NoOfRooms () {return Rooms. count;} public string Description () {IDictionaryEnumerator myEnumerator = Rooms. getEnumerator (); string strDescription = "This house total" + Rooms. count + "\ n"; while (myEnumerator. moveNext () {strDescription = strDescription + "\ n" + myEnumerator. key + "\ t" + (CRoom) myEnumerator. value ). roomName ;}return strDescription ;}}

3. inherit from the IHouse interface. The specific builder creates a house which only includes three rooms: the bedroom, the living room, and the kitchen.

/// <Summary> /// other specific builders /// </summary> public class CApt: IHouse {private bool extends nbackyard; private Hashtable Rooms; public CApt () {Rooms = new Hashtable (); CRoom room = new CRoom (); room. roomName = "bedroom"; Rooms. add ("room1", room); room = new CRoom (); room. roomName = "living room"; Rooms. add ("room2", room); room = new CRoom (); room. roomName = "Kitchen"; Rooms. add ("room3", room); required nbackyard = false;} public bool GetBackyard () {return required nbackyard;} public long NoOfRooms () {return Rooms. count;} public string Description () {IDictionaryEnumerator myEnumerator = Rooms. getEnumerator (); string strDescription = "This house total" + Rooms. count + "\ n"; while (myEnumerator. moveNext () {strDescription = strDescription + "\ n" + myEnumerator. key + "\ t" + (CRoom) myEnumerator. value ). roomName ;}return strDescription ;}}

4. Create a mentor to instruct the builder to build the desired room.

/// <Summary> // instructor // </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 objdire= new CDirector (); // instantiate IHouse objHouse; string Input = Console. readLine (); // input conditions to instruct the creator of the room to create objHouse = objDirector. buildHouse (bool. parse (Input); Console. writeLine (objHouse. description (); Console. readLine ();}

The builder mode is mainly used to "build a complex object by step". In this mode, "Step by Step" is a stable algorithm, while all parts of a complex object change frequently.

Abstract classes are not required for products, especially when the algorithm used to create objects is complex, or this mode is applied to the product generation process, the final results may vary greatly, it is unlikely to extract an abstract product category.

The preceding abstract factory model solves the demand changes of "series objects" and the Builder model solves the demand changes of "Object parts.

The builder mode allows the internal appearance of the product to change independently. Using the builder mode, the client does not have to know the details of the product's internal composition.

Each Builder is independent of other builders.

The builder mode applies to the dependency between the attributes of the product objects to be generated, and the builder mode forces the generation order. The product objects to be generated have a complex internal structure.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.