Adapter Design Pattern
suggest change“Adapter” as the name suggests is the object which lets two mutually incompatible interfaces communicate with each other.
For example: if you buy an iPhone 8 (or any other Apple product) you need a lot of adapters, because the default interface does not support an audio jack or USB. With these adapters you can use wired earphones or a normal Ethernet cable. So “two mutually incompatible interfaces communicate with each other.”
In technical terms this means: convert the interface of a class into another interface that a client expects. Adapters let classes work together that couldn’t otherwise because of incompatible interfaces. The classes and objects participating in this pattern are:
The adapter pattern consists of four elements
- ITarget: This is the interface which is used by the client to achieve functionality.
- Adaptee: This is the functionality which the client desires but its interface is not compatible with the client.
- Client: This is the class which wants to achieve some functionality by using the adaptee’s code.
- Adapter: This is the class which would implement ITarget and would call the Adaptee code which the client wants to call.
UML
First code Example (Theoretical example).
public interface ITarget
{
void MethodA();
}
public class Adaptee
{
public void MethodB()
{
Console.WriteLine("MethodB() is called");
}
}
public class Client
{
private ITarget target;
public Client(ITarget target)
{
this.target = target;
}
public void MakeRequest()
{
target.MethodA();
}
}
public class Adapter : Adaptee, ITarget
{
public void MethodA()
{
MethodB();
}
}
Second code example (real-world implementation)
/// <summary>
/// Interface: This is the interface which is used by the client to achieve functionality.
/// </summary>
public interface ITarget
{
List<string> GetEmployeeList();
}
/// <summary>
/// Adaptee: This is the functionality which the client desires but its interface is not compatible with the client.
/// </summary>
public class CompanyEmployees
{
public string[][] GetEmployees()
{
string[][] employees = new string[4][];
employees[0] = new string[] { "100", "Deepak", "Team Leader" };
employees[1] = new string[] { "101", "Rohit", "Developer" };
employees[2] = new string[] { "102", "Gautam", "Developer" };
employees[3] = new string[] { "103", "Dev", "Tester" };
return employees;
}
}
/// <summary>
/// Client: This is the class which wants to achieve some functionality by using the adaptee’s code (list of employees).
/// </summary>
public class ThirdPartyBillingSystem
{
/*
* This class is from a third party and you don't have any control over it.
* But it requires an employee list to do its work
*/
private ITarget employeeSource;
public ThirdPartyBillingSystem(ITarget employeeSource)
{
this.employeeSource = employeeSource;
}
public void ShowEmployeeList()
{
// call the client list through the interface
List<string> employee = employeeSource.GetEmployeeList();
Console.WriteLine("######### Employee List ##########");
foreach (var item in employee)
{
Console.Write(item);
}
}
}
/// <summary>
/// Adapter: This is the class which would implement ITarget and would call the Adaptee code which the client wants to call.
/// </summary>
public class EmployeeAdapter : CompanyEmployees, ITarget
{
public List<string> GetEmployeeList()
{
List<string> employeeList = new List<string>();
string[][] employees = GetEmployees();
foreach (string[] employee in employees)
{
employeeList.Add(employee[0]);
employeeList.Add(",");
employeeList.Add(employee[1]);
employeeList.Add(",");
employeeList.Add(employee[2]);
employeeList.Add("\n");
}
return employeeList;
}
}
///
/// Demo
///
class Programs
{
static void Main(string[] args)
{
ITarget Itarget = new EmployeeAdapter();
ThirdPartyBillingSystem client = new ThirdPartyBillingSystem(Itarget);
client.ShowEmployeeList();
Console.ReadKey();
}
}
When to use
- Allow a system to use classes of another system that is incompatible with it.
- Allow communication between a new system and an existing system that are independent of each other.
- ADO.NET
SqlDataAdapter,OracleDataAdapter, andMySqlDataAdapterare examples of the Adapter pattern.