In this post, we will see what the Adapter Pattern is and how we can implement it.
But first of all, what is the Adapter Patterns?
“The Adapter Design Pattern converts the interface of a class into another interface that a client expects. It allows classes to work together that couldn’t otherwise because of incompatible interfaces. The Adapter pattern is often used to make existing classes work with others without modifying their source code.”
In a nutshell, the Adapter Patter helps in making two incompatible interfaces work together, ensuring that existing code can be reused and extended without modification.
The Adapter Pattern involves four key components:
Target: This is the interface that the client expects.
Client: The client interacts with the Target interface.
Adaptee: This is the existing class that needs to be adapted.
Adapter: This is the class that bridges the gap between the Target and the Adaptee.
Let’s implement a simple example.
Imagine we have an existing application that uses a simple ILogger interface for logging messages.
We now want to use a new, more advanced logging library that provides more functionality but has a different interface. The Adapter Design Pattern can help bridge the gap between the old ILogger interface and the new logging library.
TARGET (ILogger.cs):
namespace Adapter;
public interface ILogger
{
void LogMessage(string message);
void LogError(string message);
}
CLIENT (Logger.cs):
namespace Adapter;
public class Logger: ILogger
{
public void LogMessage(string message)
{
Console.WriteLine($"This message:'{message}' is a LogMessage");
}
public void LogError(string message)
{
Console.WriteLine($"This message:'{message}' is a LogError");
}
}
Finally, we modify the Program.cs file and then we run the application:
using Adapter;
ILogger objLogger = new Logger();
objLogger.LogMessage("Message created for a message");
objLogger.LogError("Message created for an error");
Now, using the Adapter Pattern, we will see how to use an advanced logging class called AdvancedLogger, in the ILogger interface:
ADAPTEE (AdvancedLogger.cs):
namespace Adapter;
public class AdvancedLogger
{
public void LogInfo(string info)
{
Console.WriteLine($"INFO: {info}");
}
public void LogWarning(string warning)
{
Console.WriteLine($"WARNING: {warning}");
}
public void LogException(Exception ex)
{
Console.WriteLine($"EXCEPTION: {ex.Message}");
}
}
ADAPTER (LoggerAdapter.cs):
namespace Adapter;
public class LoggerAdapter(AdvancedLogger advancedLogger) : ILogger
{
public void LogMessage(string message)
{
advancedLogger.LogInfo(message);
}
public void LogError(string message)
{
advancedLogger.LogWarning(message);
}
}
Finally, we modify the Program.cs file and run the application:
using Adapter;
ILogger objLogger = new LoggerAdapter(new AdvancedLogger());
objLogger.LogMessage("Message created for a message");
objLogger.LogError("Message created for an error");
The Adapter Design Pattern is an essential tool for developers, providing a way to integrate new components into existing systems without major code changes. By using this pattern, we can ensure that our applications remain flexible and maintainable, allowing you to adopt new technologies with ease.