Here is a clean, simple explanation with formatted code and key points added 👇
❌ Without Adapter
Old class:
class OldLogger
{
public void WriteLog(string message)
{
Console.WriteLine(message);
}
}
Client:
class NewApp
{
OldLogger logger = new OldLogger();
public void Process()
{
logger.WriteLog("Processing");
}
}
👉 Problem:
NewApp is directly dependent on OldLogger → tightly coupled → hard to replace or test
✅ With Adapter
Target interface:
interface ILogger
{
void Log(string message);
}
Old class (unchanged):
class OldLogger
{
public void WriteLog(string message)
{
Console.WriteLine(message);
}
}
Adapter:
class LoggerAdapter : ILogger
{
private OldLogger oldLogger;
public LoggerAdapter(OldLogger oldLogger)
{
this.oldLogger = oldLogger;
}
public void Log(string message)
{
oldLogger.WriteLog(message);
}
}
Client:
class NewApp
{
private ILogger logger;
public NewApp(ILogger logger)
{
this.logger = logger;
}
public void Process()
{
logger.Log("Processing");
}
}
Usage:
var app = new NewApp(new LoggerAdapter(new OldLogger()));
✅ Why we need Adapter
Used when an existing class cannot be changed but its method does not match what your system expects. Adapter converts one interface into another so both can work together.
✅ Use of Adapter
It helps integrate legacy code, third-party libraries, or different systems without modifying them. It keeps your main code clean because the client works only with an interface, not the actual old class.
✅ Final Idea
Without adapter → client depends on concrete class
With adapter → client depends on interface, adapter handles conversion
👉 Adapter hides incompatibility and reduces coupling in your main logic