Adapter Design Pattern In C#

Adapter design pattern is used between incompatible interfaces. It converts the incompatible interface into a compatible interface which can be used by client. So we can say that adapter design pattern is used to allow two incompatible interface to communicate. The adapter play the role of converter or translator.

Adapter is most commonly known as Wrappers because it wraps the adapter by a new interface which can be used by the client.

To handle the incompatibility we use different approaches and based on that we can classify Adapter Pattern in 2 parts.

  1. Object Adapter Pattern
  2. Class Adapter Pattern

Object Adapter Design Pattern

In Object Adapter Pattern Incompatibility is handled by creating the object.

Class Adapter Design Pattern

In Class Adapter Design Pattern Incompatibility is handled by inheritance.

Some Real world Example

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.

Adapter pattern

Adapter

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

UML Diagram of Adapter pattern

Explaining the same thing with the code:

LumiaJSONAdaptee.CS

  1. public class LumiaJSONAdaptee  
  2. {  
  3.     public string GetLumiaMobilesJSONSpecifications()  
  4.     {  
  5.         List < LumiaMobile > LumiaMobiles = new List < LumiaMobile > ();  
  6.         LumiaMobiles.Add(new LumiaMobile  
  7.         {  
  8.             ModelId = "lumia550",  
  9.                 Height = "136.1 mm",  
  10.                 Width = "67.8 mm",  
  11.                 Thickness = "9.9 mm",  
  12.                 Weight = "141.9 g"  
  13.         });  
  14.         LumiaMobiles.Add(new LumiaMobile  
  15.         {  
  16.             ModelId = "lumia950",  
  17.                 Height = "145 mm",  
  18.                 Width = "73.2 mm",  
  19.                 Thickness = "8.2 mm",  
  20.                 Weight = "150 g"  
  21.         });  
  22.         LumiaMobiles.Add(new LumiaMobile  
  23.         {  
  24.             ModelId = "Text",  
  25.                 Height = "",  
  26.                 Width = "",  
  27.                 Thickness = "8.2 mm",  
  28.                 Weight = "150 g"  
  29.         });  
  30.         dynamic collectionLumiaMobiles = new  
  31.         {  
  32.             lumiaMobiles = LumiaMobiles  
  33.         };  
  34.         return JsonConvert.SerializeObject(collectionLumiaMobiles);  
  35.     }  
  36. }  
LumiaMobile.cs
  1. public class LumiaMobile  
  2. {  
  3.     public string ModelId  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string Height  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string Width  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string Thickness  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23.     public string Weight  
  24.     {  
  25.         get;  
  26.         set;  
  27.     }  
  28. }  
ILumiaXMLTarget.cs
  1. interface ILumiaXMLTarget  
  2. {   
  3.    XmlDocument GetLumiaMobilesXMLSpecifications();  
  4. }  
LumiaXMLAdapter.cs
  1. class LumiaXMLAdapter : ILumiaXMLTarget  
  2. {  
  3.     public XmlDocument GetLumiaMobilesXMLSpecifications()  
  4.     {  
  5.         LumiaJSONAdaptee lumiaJsonAdaptee = new LumiaJSONAdaptee();  
  6.         string jsonLumia = lumiaJsonAdaptee.GetLumiaMobilesJSONSpecifications();  
  7.         XmlDocument doc = JsonConvert.DeserializeXmlNode(jsonLumia, "MicrosoftLumiaMoblies",true);  
  8.         return doc;  
  9.     }  

MyLumiaClient.cs
  1. class MyLumiaClient  
  2. {  
  3.     private ILumiaXMLTarget _lumiaXmlTarget;  
  4.     public MyLumiaClient(ILumiaXMLTarget lumiaXmlTarget)  
  5.     {  
  6.         _lumiaXmlTarget = lumiaXmlTarget;  
  7.     }  
  8.     public XmlDocument GetLumiaData()  
  9.     {  
  10.         return _lumiaXmlTarget.GetLumiaMobilesXMLSpecifications();  
  11.     }  
  12. }  


Recommended Ebook

Working with Directories in C#

Download Now!
Similar Articles