04 Dec 2023
The Builder design pattern is a creational pattern that separates the construction of a complex object from its representation. It allows the same construction process to create various representations. In C#, the Builder pattern typically involves the following main components:
-
Builder Interface/Abstract Class:
- Defines an interface or abstract class for creating parts of the complex object.
- Declares abstract methods that are responsible for building individual components.
public interface IBuilder { void BuildPart1(); void BuildPart2(); // ... other methods for building parts Product GetResult(); } -
Concrete Builder:
- Implements the Builder interface.
- Constructs and assembles parts of the complex object by implementing the interface methods.
- Maintains an instance of the product being built.
public class ConcreteBuilder : IBuilder { private Product product = new Product(); public void BuildPart1() { // Implementation to build part 1 product.AddPart("Part 1"); } public void BuildPart2() { // Implementation to build part 2 product.AddPart("Part 2"); } public Product GetResult() { return product; } } -
Director:
- Manages the construction process using a builder.
- Directs the order in which the builder constructs the parts.
- Typically, the client interacts with the director to create the complex object.
public class Director { private IBuilder builder; public Director(IBuilder builder) { this.builder = builder; } public void Construct() { builder.BuildPart1(); builder.BuildPart2(); // ... other building steps } } -
Product:
- Represents the complex object being constructed.
- The final product that the client wants to obtain.
public class Product { private List<string> parts = new List<string>(); public void AddPart(string part) { parts.Add(part); } public void Show() { Console.WriteLine("Product Parts: " + string.Join(", ", parts)); } } -
Client:
- Asks the Director to build the product using a specific builder.
- Gets the final product from the builder.
class Client { static void Main() { IBuilder builder = new ConcreteBuilder(); Director director = new Director(builder); director.Construct(); Product product = builder.GetResult(); product.Show(); } }
In the above code, the construction and representation aspects of the Builder design pattern are as follows:
-
Construction:
- The construction aspect is primarily handled by the
ConcreteBuilderclass. This class implements theIBuilderinterface and is responsible for constructing and assembling the parts of the complex object (Product). Each method in theIBuilderinterface (BuildPart1,BuildPart2, etc.) represents a step in the construction process.
public class ConcreteBuilder : IBuilder { private Product product = new Product(); public void BuildPart1() { // Implementation to build part 1 product.AddPart("Part 1"); } public void BuildPart2() { // Implementation to build part 2 product.AddPart("Part 2"); } public Product GetResult() { return product; } } - The construction aspect is primarily handled by the
-
Representation:
- The representation aspect is reflected in the
Productclass. This class represents the complex object being constructed, and it has methods for adding parts (AddPart) and displaying the final result (Show).
public class Product { private List<string> parts = new List<string>(); public void AddPart(string part) { parts.Add(part); } public void Show() { Console.WriteLine("Product Parts: " + string.Join(", ", parts)); } }The
Clientinteracts with the director (Director) to initiate the construction process using a specific builder (ConcreteBuilder). The final product is obtained from the builder, and the representation of the product is displayed.class Client { static void Main() { IBuilder builder = new ConcreteBuilder(); Director director = new Director(builder); director.Construct(); Product product = builder.GetResult(); product.Show(); } }The
Directorclass manages the construction process by directing the order in which the builder constructs the parts. The client can obtain the final product with its desired representation without having detailed knowledge of the construction steps. This separation of construction and representation allows flexibility in creating different representations of the complex object. - The representation aspect is reflected in the
The Builder pattern allows you to vary the representation of the product by using different builders without changing the client code. It promotes the construction of a complex object step by step and provides better control over the construction process.