Builder Pattern in .NET Core 7
Introduction
The Builder design pattern is a creational pattern used to construct complex objects step by step. It separates the construction of an object from its representation, allowing you to create different representations of an object using the same construction process. In this example, I’ll demonstrate how to implement the Builder pattern for creating fruit objects in .NET Core.
Prerequisites
Before we dive into implementing the Abstract Factory Pattern, let’s set up our .NET Core project in Visual Studio Code.:
- .NET Core SDK for building and running .NET Core applications.
- Visual Studio Code for code editing
Create a .NET Core Console Application
Let’s start by creating a new .NET Core console application. Open your terminal and run the following commands:
dotnet new console -n FruitBuilder
cd FruitBuilder
This will create a new console application named FruitBuilder
.
Open the Project in Visual Studio Code:
code .
This will open the project in VS Code
Now that we have our project ready, let’s proceed with implementing the Builder Pattern for fruits.
Implementation
Let’s start with a simple Fruit
class:
public class Fruit
{
public string Name { get; set; }
public string Color { get; set; }
public double Weight { get; set; }
public string Taste { get; set; }
}
Now, let’s create a builder for the Fruit
class:
public class FruitBuilder
{
private Fruit _fruit = new Fruit()
public FruitBuilder WithName(string name)
{
_fruit.Name = name;
return this;
}
public FruitBuilder WithColor(string color)
{
_fruit.Color = color;
return this;
}
public FruitBuilder WithWeight(double weight)
{
_fruit.Weight = weight;
return this;
}
public FruitBuilder WithTaste(string taste)
{
_fruit.Taste = taste;
return this;
}
public Fruit Build()
{
return _fruit;
}
}
With the FruitBuilder
in place, you can now create Fruit
objects step by step:
class Program
{
static void Main(string[] args)
{
// Create a fruit using the builder
Fruit apple = new FruitBuilder()
.WithName("Apple")
.WithColor("Red")
.WithWeight(0.2)
.WithTaste("Sweet")
.Build();
// Create another fruit
Fruit banana = new FruitBuilder()
.WithName("Banana")
.WithColor("Yellow")
.WithWeight(0.15)
.WithTaste("Sweet")
.Build();
// Print the fruit information
Console.WriteLine($"Name: {apple.Name}, Color: {apple.Color}, Weight: {apple.Weight} kg, Taste: {apple.Taste}");
Console.WriteLine($"Name: {banana.Name}, Color: {banana.Color}, Weight: {banana.Weight} kg, Taste: {banana.Taste}");
}
}
In this example, we’ve created a FruitBuilder
class that allows you to set the properties of a Fruit
object step by step using a fluent API. This separates the construction of the Fruit
object from its representation, making it easier to create complex objects with various attributes in a clean and maintainable way.
You can follow the instruction and create a project at your own or clone the repository at https://gitlab.com/mvineetsharma/microsoft/design-patterns/builder-pattern for code in action