Adapter Pattern in .NET Core 7
Introduction
In software development, design patterns are essential tools for creating reusable and maintainable code. One such pattern is the Adapter Method Pattern, which allows you to adapt the interface of one class to another without changing its source code. In this blog post, we’ll explore how to implement the Adapter Method Pattern for a collection of fruits in a .NET Core application.
Scenario
Suppose you have an existing FruitService class that provides various operations on fruits, like adding, removing, and listing fruits. Now, you want to integrate a new FruitVendor class into your application, which has a different interface for managing fruits. Instead of modifying the existing FruitService, you can use the Adapter Method Pattern to make these two classes work together seamlessly.
Implementation Steps:
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 FruitAdapter
cd FruitAdapter
This will create a new console application named FruitAdapter
.
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 Adapter Pattern for fruits.
Implementation Steps:
- Define the Target Interface: Start by defining an interface that your existing class (FruitService) can understand. This interface represents the operations you need to perform on fruits. Create a file called
IFruitAdapter.cs
with the following code:
public interface IFruitAdapter
{
void AddFruit(string fruitName);
void RemoveFruit(string fruitName);
List<string> GetAllFruits();
}
- Create the Adapter Class: Next, create an adapter class that implements the
IFruitAdapter
interface and adapts the FruitVendor class to work with it. Let's call this classFruitVendorAdapter
. Here's the code for the adapter:
public class FruitVendorAdapter : IFruitAdapter
{
private FruitVendor _fruitVendor;
public FruitVendorAdapter(FruitVendor fruitVendor)
{
_fruitVendor = fruitVendor;
}
public void AddFruit(string fruitName)
{
_fruitVendor.PurchaseFruit(fruitName);
}
public void RemoveFruit(string fruitName)
{
_fruitVendor.SellFruit(fruitName);
}
public List<string> GetAllFruits()
{
return _fruitVendor.GetAvailableFruits();
}
}
- Integrate the Adapter: In your application, instantiate the
FruitVendor
andFruitVendorAdapter
classes, and use the adapter to perform operations on fruits without modifying your existingFruitService
. Here's how you can do it:
class Program
{
static void Main()
{
// Existing FruitService
var fruitService = new FruitService();
// New FruitVendor
var fruitVendor = new FruitVendor();
// Adapter for FruitVendor
var adapter = new FruitVendorAdapter(fruitVendor);
// Use the adapter to interact with FruitVendor
adapter.AddFruit("Apple");
adapter.AddFruit("Banana");
adapter.RemoveFruit("Orange");
// Use existing FruitService
fruitService.AddFruit("Grapes");
fruitService.RemoveFruit("Apple");
// Get a list of all fruits from both sources
var allFruits = adapter.GetAllFruits();
allFruits.AddRange(fruitService.GetAllFruits());
// Display all fruits
Console.WriteLine("All Fruits:");
foreach (var fruit in allFruits)
{
Console.WriteLine(fruit);
}
}
}
Conclusion
The Adapter Method Pattern allows you to integrate new functionality (in this case, the FruitVendor
class) into your existing codebase without making significant changes. By defining an interface (IFruitAdapter
) and creating an adapter class (FruitVendorAdapter
), you can adapt the interface of the new class to match the expectations of your existing code. This promotes code reusability, maintainability, and flexibility in your .NET Core application.
You can follow the instruction and create a project at your own or clone the repository at https://gitlab.com/mvineetsharma/microsoft/design-patterns/adapter-pattern for code in action.