Abstract Factory Pattern .NET Core 7
Introduction
The Abstract Factory Pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. In this blog post, we’ll explore how to implement the Abstract Factory Pattern for creating different types of fruits in a .NET Core application using Visual Studio Code.
Why Use the Abstract Factory Pattern?
Imagine you are developing a fruit processing application, and you need to create various types of fruits, such as apples, oranges, and bananas. Each type of fruit may have different properties and methods, but you want to abstract away the details of how these fruits are created. This is where the Abstract Factory Pattern comes in handy. It allows you to define a factory interface that can create families of related objects, like fruits, without exposing the concrete implementations.
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 FruitFactory
cd FruitFactory
This will create a new console application named FruitFactory
.
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 Abstract Factory Pattern for creating fruits.
Implementing the Abstract Factory Pattern
Define the Fruit Interfaces
In our project, we’ll define two interfaces: IFruit.cs
and IFruitFactory.cs
. IFruit
represents the common properties and methods that all fruits should have, while IFruitFactory
is responsible for creating different types of fruits.
public interface IFruit
{
string Name { get; }
void Grow();
void Harvest();
}
public interface IFruitFactory
{
IFruit CreateFruit();
}
Create Concrete Fruit Classes
Next, we’ll create concrete fruit classes that implement the IFruit
interface. For example, let's create classes for apples Apple.cs
, oranges, and bananas.
public class Apple : IFruit
{
public string Name => "Apple";public void Grow()
{
Console.WriteLine("Apple is growing...");
}public void Harvest()
{
Console.WriteLine("Apple has been harvested.");
}
}
// Similar classes for Orange and Banana
Implement Concrete Fruit Factories
Now, we’ll create concrete fruit factory classes AppleFactory.cs
that implement the IFruitFactory
interface. Each factory is responsible for creating a specific type of fruit.
public class AppleFactory : IFruitFactory
{
public IFruit CreateFruit()
{
return new Apple();
}
}
// Similar factories for Orange and Banana
Client Code
In our client code, we can use the abstract factory to create fruits without knowing the concrete types.
using FruitFactory;
public class Program
{
public static void Main(string[] args)
{
IFruitFactory factory = new AppleFactory();
IFruit apple = factory.CreateFruit();
Console.WriteLine($"Created a {apple.Name}");
apple.Grow();
apple.Harvest();
}
}
Running the Application
To run the application, open a terminal in Visual Studio Code and execute the following command:
dotnet run
You should see output similar to:
Created a Apple
Apple is growing...
Apple has been harvested.
Conclusion
In this blog post, we explored how to implement the Abstract Factory Pattern in a .NET Core application using Visual Studio Code. The Abstract Factory Pattern allows us to create families of related objects, such as fruits, in a way that is abstracted from the concrete implementations. This promotes flexibility and maintainability in our codebase, making it easier to add new types of fruits or change the way they are created without affecting the client code.
You can follow the instruction and create a project at your own or clone the repository at https://gitlab.com/mvineetsharma/microsoft/design-patterns/abstract-factory-pattern for code in action.