Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the .NET Builder pattern?

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What is the .NET builder model, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Definition of the builder pattern:

A design pattern that separates the construction of a complex object from its representation so that the same construction process can create different representations is called the builder pattern.

Builder pattern structure diagram:

Builder mode role:

1 builder: specifies an abstract interface for each part that creates a product object.

2 ConcreteBuilder: implement the interface of Builder to construct and assemble the various parts of the product, define and define the representations it creates, and provide an interface to retrieve the product.

3 Director: construct 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, including classes that define the components, including interfaces to assemble those parts into the final product.

The following is an example of building a house in real life to illustrate the builder model:

1. Abstract the builder interface, the conditions to be implemented to create the type of house, the number of rooms returned after creation, and the description of the house.

/ Abstract builder / public interface IHouse {/ conditions for creating house types / bool GetBackyard (); / long NoOfRooms () / string Description ();}

two。 Inherit the IHouse interface, the specific builder, here to create a room, including the living room, kitchen, bathroom, bedroom, a total of four rooms such a house.

Public class CRoom {public string RoomName {get; set;}} / specific builder / public class CSFH:IHouse {private bool mblnBackyard; 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 = "first floor kitchen"; Rooms.Add ("room2", room); room = new CRoom () Room.RoomName = "first floor bathroom"; Rooms.Add ("room3", room); room = new CRoom (); room.RoomName = "first floor bedroom"; Rooms.Add ("room4", room); mblnBackyard = true;} public bool GetBackyard () {return mblnBackyard;} public long NoOfRooms () {return Rooms.Count;} public string Description () {IDictionaryEnumerator myEnumerator = Rooms.GetEnumerator (); string strDescription = "this house" + Rooms.Count + "room\ n" While (myEnumerator.MoveNext ()) {strDescription = strDescription + "\ n" + myEnumerator.Key + "\ t" + ((CRoom) myEnumerator.Value) .RoomName;} return strDescription;}}

3. Inherit the IHouse interface, the specific builder, here to create a house, which only includes the bedroom, living room, kitchen a total of three rooms such a house.

/ other specific builders / public class CApt:IHouse {private bool mblnBackyard; 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) 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"; while (myEnumerator.MoveNext ()) {strDescription = strDescription + "\ n" + myEnumerator.Key + "\ t" + ((CRoom) myEnumerator.Value) .RoomName;} return strDescription;}}

4. Create a mentor, instructing which builder is required to build what kind of room.

/ instructor / 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 (); / / instantiator IHouse objHouse; string Input = Console.ReadLine (); / / enter conditions to instruct which creator to create a room objHouse = objDirector.BuildHouse (bool.Parse (Input)); Console.WriteLine (objHouse.Description ()); Console.ReadLine ();}

The builder pattern is mainly used to "build a complex object step by step", in which "step by step" is a stable algorithm, while each part of the complex object changes frequently.

Products do not need abstract classes, especially when this pattern is used or applied to the product generation process due to the complexity of the algorithm for creating objects, the final results may vary greatly and it is unlikely to extract an abstract product class.

The previous abstract factory pattern addresses the changes in the requirements of the "series of objects", while the Builder pattern addresses the changes in the requirements of the "object part".

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

Each Builder is relatively independent and independent of other Builder.

The builder pattern applies to the interdependence 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.

After reading the above, have you mastered the method of .NET Builder pattern? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report