Introduction
An adapter design pattern is used between incompatible interfaces. It converts the incompatible interface into a compatible interface that can be used by the client. So we can say that an adapter design pattern is used to allow two incompatible interfaces to communicate. The adapter plays the role of converter or translator.
The adapter is most commonly known as Wrappers because it wraps the adapter with a new interface that can be used by the client.
To handle the incompatibility, we use different approaches, and based on that, we can classify the Adapter Pattern into 2 parts.
- Object Adapter Pattern
- Class Adapter Pattern
Object Adapter Design Pattern
In Object Adapter Pattern, Incompatibility is handled by creating the object.
Class Adapter Design Pattern
Class Adapter Design Pattern Incompatibility is handled by inheritance.
Some Real-world Examples
The following image is my personal use of LED Monitor. I use my monitor in different ways, which you can see in the following image.


Let me explain the UML Diagram of the Adapter pattern using a real example of programming. I have an interface that returns a list of Lumia Mobiles in JSON, but the Client is expecting the list of Lumia Mobile features in XML format. Let‘s solve it using the Adapter patter.

LumiaJSONAdaptee.CS
public class LumiaJSONAdaptee
{
public string GetLumiaMobilesJSONSpecifications()
{
List<LumiaMobile> LumiaMobiles = new List<LumiaMobile>();
LumiaMobiles.Add(new LumiaMobile
{
ModelId = "lumia550",
Height = "136.1 mm",
Width = "67.8 mm",
Thickness = "9.9 mm",
Weight = "141.9 g"
});
LumiaMobiles.Add(new LumiaMobile
{
ModelId = "lumia950",
Height = "145 mm",
Width = "73.2 mm",
Thickness = "8.2 mm",
Weight = "150 g"
});
LumiaMobiles.Add(new LumiaMobile
{
ModelId = "Text",
Height = "",
Width = "",
Thickness = "8.2 mm",
Weight = "150 g"
});
dynamic collectionLumiaMobiles = new
{
lumiaMobiles = LumiaMobiles
};
return JsonConvert.SerializeObject(collectionLumiaMobiles);
}
}
LumiaMobile.cs
public class LumiaMobile
{
public string ModelId { get; set; }
public string Height { get; set; }
public string Width { get; set; }
public string Thickness { get; set; }
public string Weight { get; set; }
}
ILumiaXMLTarget.cs
interface ILumiaXMLTarget
{
XmlDocument GetLumiaMobilesXMLSpecifications();
}
LumiaXMLAdapter.cs
class LumiaXMLAdapter : ILumiaXMLTarget
{
public XmlDocument GetLumiaMobilesXMLSpecifications()
{
LumiaJSONAdaptee lumiaJsonAdaptee = new LumiaJSONAdaptee();
string jsonLumia = lumiaJsonAdaptee.GetLumiaMobilesJSONSpecifications();
XmlDocument doc = JsonConvert.DeserializeXmlNode(jsonLumia, "MicrosoftLumiaMobiles", true);
return doc;
}
}
class MyLumiaClient
{
private ILumiaXMLTarget _lumiaXmlTarget;
public MyLumiaClient(ILumiaXMLTarget lumiaXmlTarget)
{
_lumiaXmlTarget = lumiaXmlTarget;
}
public XmlDocument GetLumiaData()
{
return _lumiaXmlTarget.GetLumiaMobilesXMLSpecifications();
}
}

Ammar ShaukatPosted Aug 10, 2024, 1:08 PM
It would be great if you enhance your code example.. anyway you did a great job by wiritng example from daily life.
Ammar ShaukatPosted Aug 10, 2024, 12:29 PM
Nice share
Banketeshvar NarayanPosted Dec 3, 2015, 11:02 AM
Simion Dragos , you are right. same code were copied twice. even though I had attached a downloadable source which has correct. Thanks for notifying me. I have made the changes. Thanks a lot
Simion DragosPosted Dec 2, 2015, 4:33 AM
Hi Banketeshvar Narayan, could you check the code for the LumiaXMLAdapter.cs class? It seesm that you pasted the same code for both, LumiaXMLAdapter.cs and MyLumiaClient.cs.
Banketeshvar NarayanPosted Nov 30, 2015, 5:48 AM
Thanks Suman
Suman VermaPosted Nov 30, 2015, 5:48 AM
nice one
Banketeshvar NarayanPosted Nov 30, 2015, 2:29 AM
Thanks Aishwarya
Aishwarya DPosted Nov 30, 2015, 1:46 AM
Nice one
Banketeshvar NarayanPosted Nov 18, 2015, 10:59 AM
As I have discussed in this article for decompiling the code in response to "Gul Md Ershad" query... if any one is having trouble to decompile code you can go through my new article http://www.c-sharpcorner.com/UploadFile/efa3cf/decompile-an-assembly-in-C-Sharp/
Banketeshvar NarayanPosted Nov 18, 2015, 10:53 AM
Thanks Yaduveer
Yaduveer SainiPosted Nov 18, 2015, 9:25 AM
Nice Article
Banketeshvar NarayanPosted Nov 17, 2015, 2:30 PM
Thanks Shridhar Sharma
Banketeshvar NarayanPosted Nov 17, 2015, 2:29 PM
Thanks Santhakumar Munuswamy
Banketeshvar NarayanPosted Nov 17, 2015, 2:28 PM
Gul Md Ershad Thanks a lot for this healthy discussion.
Shridhar SharmaPosted Nov 17, 2015, 1:06 PM
nice share
Santhakumar MunuswamyPosted Nov 17, 2015, 8:31 AM
Good one
Former memberPosted Nov 17, 2015, 6:56 AM
Is calling COM from .NET an example of Adapter pattern?
Former memberPosted Nov 17, 2015, 6:55 AM
Where is the adapter pattern used in .NET?
Former memberPosted Nov 17, 2015, 6:52 AM
Does Adapter pattern conflict with encapsulation?
Banketeshvar NarayanPosted Nov 17, 2015, 5:10 AM
Thanks Rajeesh
Banketeshvar NarayanPosted Nov 17, 2015, 5:10 AM
Thanks Sibeesh
Banketeshvar NarayanPosted Nov 17, 2015, 5:10 AM
Harshad Pansuriya Thank you
Rajeesh MenothPosted Nov 17, 2015, 4:32 AM
Thanks for sharing
Sibeesh VenuPosted Nov 17, 2015, 3:55 AM
Nice Share
Harshad PansuriyaPosted Nov 17, 2015, 3:28 AM
Nice one
Banketeshvar NarayanPosted Nov 17, 2015, 1:59 AM
@Suman Verma Thank you
Suman VermaPosted Nov 17, 2015, 1:55 AM
Nice Article