Adpater pattern converts the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
What is the practical usage of this pattern? Many frameworks use this pattern to convert different interfaces written in different languages so that they can communicate seamlessly and transparently with each other
Client.cs
using System;
using BusinessLogic;
namespace Test
{
public class Client
{
public static void Main(string[] args)
{
VoltageAdapter Adapter = new VoltageAdapter();
Console.WriteLine(Adapter.Request(0));
Console.WriteLine(Adapter.Request(1));
Console.WriteLine(Adapter.Request(13));
}
}
}
BL.cs
using System;
namespace BusinessLogic
{
// Clients have no access to this class, but need its functionalities
internal class ElectricShaver
{
internal string Request(int cable)
{
string Status = "Not yet plug in";
switch(cable)
{
case 0:
Status = "110 Voltage goes here";
break;
case 1:
Status = "220 voltage goes here";
break;
default:
Status = "Kaboom";
break;
}
return Status;
}
}
// The VoltageAdapter is the adpater class providing access
// to any internal or external interfaces that clients expect
public class VoltageAdapter
{
private ElectricShaver adaptee = new ElectricShaver();
public string Request(int cable)
{
return adaptee.Request(cable);
}
}
}
Compile the code as below:
csc /t:library BL.cs
csc /r:BL.dll Adapter.cs
Love to hear your point of view about this pattern.
Hope it helps