Builder - Creational
Overview:
The Builder Pattern is an object-oriented design pattern that separates the construction of a complex object from its representation, allowing the same construction process to create different representations. Let's dive into its details:
Implementation in C#:
In C#, the Builder Pattern typically involves a director-class orchestrating a builder interface to construct a complex object. Here's a basic example:
// Product class
public class Sandwich
{
public string Bread { get; set; }
public string Meat { get; set; }
public string Cheese { get; set; }
public string Veggies { get; set; }
}
// Builder interface
public interface ISandwichBuilder
{
void AddBread();
void AddMeat();
void AddCheese();
void AddVeggies();
Sandwich GetSandwich();
}
// Concrete builder
public class ClubSandwichBuilder : ISandwichBuilder
{
private Sandwich sandwich = new Sandwich();
public void AddBread() { sandwich.Bread = "Whole Wheat"; }
public void AddMeat() { sandwich.Meat = "Turkey"; }
public void AddCheese() { sandwich.Cheese = "Swiss"; }
public void AddVeggies() { sandwich.Veggies = "Lettuce, Tomato"; }
public Sandwich GetSandwich() { return sandwich; }
}
// Director class
public class SandwichMaker
{
private ISandwichBuilder builder;
public SandwichMaker(ISandwichBuilder builder)
{
this.builder = builder;
}
public void BuildSandwich()
{
builder.AddBread();
builder.AddMeat();
builder.AddCheese();
builder.AddVeggies();
}
}
Pros:
-
Separation of Concerns: Separates the construction process from the actual representation, promoting modularity.
-
Flexibility: Allows the same construction process to create different representations.
-
Complex Object Handling: Handles the construction of complex objects with varying components.
Cons:
-
Increased Code Overhead: Introducing builders may lead to increased code complexity.
-
Learning Curve: Developers new to the pattern may require time to understand its structure and benefits.
When to Use and When Not:
-
Use: When constructing complex objects with many components and you want to vary the representation.
-
Avoid: In scenarios where object construction is straightforward and doesn't involve varying representations.
Usage in .NET Core Framework:
While the Builder Pattern itself might not be explicitly used in the .NET Core framework, its principles are evident in various areas. For example, the HttpClientBuilder in ASP.NET Core allows flexible configuration of HttpClient instances.
Real-Life Example:
Imagine configuring an HttpClient with specific headers and timeouts:
var httpClient = new HttpClientBuilder()
.WithBaseAddress("https://api.example.com")
.WithTimeout(TimeSpan.FromSeconds(30))
.WithHeader("Authorization", "Bearer Token123")
.Build();
This fluent API reflects the Builder Pattern's influence, offering a structured and customizable approach to construct HttpClient instances.
Registering with Core Framework:
To register a builder in .NET Core's DI container, use the AddScoped, AddTransient, or AddSingleton method based on the desired scope. For instance, if HttpClientBuilder were a service:
public void ConfigureServices(IServiceCollection services)
{
// Other service registrations...
services.AddScoped<HttpClientBuilder>();
}
In conclusion, the Builder Pattern in C# empowers developers to construct complex objects with finesse. While it introduces flexibility and a step-by-step approach, understanding when to apply it is crucial for effective and modular software design. The pattern's principles resonate in various areas of .NET Core, showcasing its enduring impact on crafting elegant and configurable code structures.
No files yet, migration hasn't completed yet!