English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Explanation of .NET Builder Pattern

Definition of Builder pattern:

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

Builder pattern structure diagram:

Builder pattern roles:

1 Builder: Specifies an abstract interface for specifying the components of a product object to be created.
2 ConcreteBuilder: Implements the Builder interface to construct and assemble the various components of the product, defines and clearly states the representation it creates, and provides an interface to retrieve the product.
3 Director: Constructs an object using the Builder interface.
4 Product: Represents the complex object to be constructed. ConcreteBuilder creates the internal representation of the product and defines its assembly process, including defining classes for the components and interfaces for assembling these components into the final product.

Below is an example of building a house in real life to illustrate the Builder pattern:

1The builder interface is abstracted out, which includes conditions to be implemented for creating house types, returns the number of rooms created after creation, and provides the description information of this house.

 /// <summary>
 /// Abstract Builder
 /// </summary>
 public interface IHouse
 {
 /// <summary>
 /// Conditions for creating house types
 /// </summary>
 /// <returns></returns>
 bool GetBackyard();
 /// <summary>
 /// Number of rooms created
 /// </summary>
 /// <returns></returns>
 long NoOfRooms();
 /// <summary>
 /// Description
 /// </summary>
 /// <returns></returns>
 string Description();
 

2This builder implements the IHouse interface and creates a room, which includes living room, kitchen, bathroom, bedroom, a total of four rooms in this house.

 public class CRoom
 {
 public string RoomName { get; set; }
 
 /// <summary>
 /// Specific Builder
 /// </summary>
 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("room",1", room);
 room = new CRoom();
 room.RoomName = "First Floor Kitchen";
 Rooms.Add("room",2", room);
 room = new CRoom();
 room.RoomName = "First Floor Bathroom";
 Rooms.Add("room",3", room);
 room = new CRoom();
 room.RoomName = "First Floor Bedroom";
 Rooms.Add("room",4",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 has a total of " + Rooms.Count + "  room \n";
 while (myEnumerator.MoveNext())
 {
 strDescription = strDescription + "\n" + myEnumerator.Key + "\t" + ((CRoom)myEnumerator.Value).RoomName; 
 
 return strDescription;
 
 

3This builder implements the IHouse interface and creates a house, which includes only three rooms: bedroom, living room, and kitchen.

 /// <summary>
 /// Other specific builders
 /// </summary>
 public class CApt : IHouse
 {
 private bool mblnBackyard;
 private Hashtable Rooms;
 public CApt()
 { 
 Rooms = new Hashtable();
 CRoom room = new CRoom();
 room.RoomName = "Bedroom";
 Rooms.Add("room",1", room);
 room = new CRoom();
 room.RoomName = "Living Room";
 Rooms.Add("room",2", room);
 room = new CRoom();
 room.RoomName = "Kitchen";
 Rooms.Add("room",3", 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 has a total of " + Rooms.Count + "  room \n";
 while (myEnumerator.MoveNext())
 {
  strDescription = strDescription + "\n" + myEnumerator.Key + "\t" + ((CRoom)myEnumerator.Value).RoomName; 
 
 return strDescription;
 
 

4.Create director, guide which builder to build what kind of room.

 /// <summary>
 /// Director
 /// </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();//Instance the director
 IHouse objHouse;
 string Input = Console.ReadLine();//Input conditions guide which creator creates the room
 objHouse = objDirector.BuildHouse(bool.Parse(Input));
 Console.WriteLine(objHouse.Description());
 Console.ReadLine();
 

Builder pattern is mainly used for 'building a complex object step by step', in which 'step by step' is a stable algorithm, and the various parts of the complex object often change

The product does not need an abstract class, especially in the case of using this pattern due to the complex algorithm of creating objects or when this pattern is applied to the generation process of the product, the final result may be very different, and it is unlikely to extract an abstract product class.

The previous abstract factory pattern solves the demand changes of 'series of objects', and the Builder pattern solves the demand changes of 'object parts'.

The use of the Builder pattern allows the internal appearance of the product to change independently. Using the Builder pattern can make the client not need to know the details of the internal composition of the product

Each Builder is relatively independent and has nothing to do with other Builders.

Builder pattern is suitable for the case where the properties of the objects to be generated are interdependent, and the Builder pattern can force the generation order. The objects to be generated have complex internal structures.

That's all for this article. Hope it helps everyone's learning and also hope everyone will support the Yell Tutorial.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any suspected copyright content, please send an email to: notice#oldtoolbag.com (When sending an email, please replace # with @ for reporting. Provide relevant evidence, and once verified, this site will immediately delete the suspected infringing content.)

You May Also Like