Factory Pattern in .NET Core 7
Introduction
The Factory Method Pattern is a creational design pattern that provides an interface for creating objects but allows subclasses to alter the type of objects that will be created. In the context of .NET Core, we can use this pattern to create fruit objects.
In this blog post, we’ll walk through the implementation of the Factory Method Pattern for creating fruit objects in .NET Core using Visual Studio Code. We’ll create a simple application that demonstrates how to use the Factory Method Pattern to create different types of fruits.
Prerequisites
Before we begin, make sure you have the following installed on your system:
- .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
.
Define the Fruit Interface
Next, let’s define an interface for our fruit objects. Create a file named IFruit.cs
in your project directory and add the following code:
public interface IFruit
{
string GetName();
}
This interface will be implemented by different types of fruits.
Create Concrete Fruit Classes
Now, let’s create some concrete fruit classes that implement the IFruit
interface. Create a file for each fruit type (e.g., Apple.cs
, Banana.cs
, and Orange.cs
) and implement the GetName
method in each class:
public class Apple : IFruit
{
public string GetName()
{
return "Apple";
}
}
public class Banana : IFruit
{
public string GetName()
{
return "Banana";
}
}
public class Orange : IFruit
{
public string GetName()
{
return "Orange";
}
}
These classes represent different types of fruits, and they provide their names when the GetName
method is called.
Create the FruitFactory Interface
Now, let’s create an interface for our factory, which will be responsible for creating fruit objects. Create a file named IFruitFactory.cs
and add the following code:
public interface IFruitFactory
{
IFruit CreateFruit();
}
This interface defines a CreateFruit
method that will be implemented by concrete fruit factories.
Implement Concrete Fruit Factories
Create concrete factories for each type of fruit. For example, create a file named AppleFactory.cs
and add the following code:
public class AppleFactory : IFruitFactory
{
public IFruit CreateFruit()
{
return new Apple();
}
}
Repeat this process for BananaFactory
and OrangeFactory
as well, each creating their respective fruit objects.
Using the Factory Method
Now, let’s use the Factory Method Pattern to create fruit objects in our Program.cs
file:
using FruitFactory;
class Program
{
static void Main()
{
IFruitFactory appleFactory = new AppleFactory();
IFruitFactory bananaFactory = new BananaFactory();
IFruitFactory orangeFactory = new OrangeFactory();IFruit apple = appleFactory.CreateFruit();
IFruit banana = bananaFactory.CreateFruit();
IFruit orange = orangeFactory.CreateFruit()
Console.WriteLine($"Apple: {apple.GetName()}");
Console.WriteLine($"Banana: {banana.GetName()}");
Console.WriteLine($"Orange: {orange.GetName()}");
}
}
In this code, we create instances of fruit factories and use them to create different fruit objects. We then call the GetName
method on each fruit object to display their names.
Running the Application
To run the application, use the following command in the terminal:
dotnet run
You should see the following output:
Apple: Apple
Banana: Banana
Orange: Orange
This demonstrates how the Factory Method Pattern can be used in .NET Core to create objects of different types in a flexible and maintainable way.
That’s it! You’ve successfully implemented the Factory Method Pattern for creating fruit objects in a .NET Core application using Visual Studio Code. This pattern allows you to abstract the object creation process and easily extend your application with new types of fruits in the future.
You can follow the instruction and create a project at your own or clone the repository at https://gitlab.com/mvineetsharma/microsoft/design-patterns/factory-pattern for code in action.