Adapter Design Pattern

Introduction 

 
In the real world, we all are familiar with an electric outlet. Every country has its own style of electric outlets. That's why smartphone manufacturers makes a different style of charging adapters which are specific to the country. One great example would be Apple's Hyperdrive. It connects so many devices to a single USB port. Here, hyperdrive is acting as an Adapter.
 
 
Adapter Design Pattern
 
The adapter also is known as a wrapper. cause it wraps some object that can be used by different objects.
 
Let's see UML first.
 
Adapter Design Pattern
  1. The client is basically the class who wants to use the pattern. The calling class.
  2. Now the client wants to have Specific_request() which can only be served by Adaptee class, we can directly have a HAS-A relationship and be done with it. but the problem lies when there is another specific_request which is served by let's say Adaptee2 class, in this case, we have to make another HAS-A relationship and this could reoccur with every new requirement.
To deal with this, we can abstract a requirement and let abstraction handle delegation of a request made by the client. In the above UML, it is handled by the ITarget interface
 
Let's jump into the coding.
 
Example
 
Let's say our application reads data from an XML file and it serves the purpose for today. Tomorrow, if we would like our application to support JSON, then what?
 
Let's solve this problem with an Adapter design pattern.
 
This is how our UML would look:
 
Adapter Design Pattern 
 
Let's create an interface ITarget which will be called by our client.
  1. public interface ITarget  
  2.   {  
  3.       string GetDetails();  
  4.   } 
Now our Adapter, which will implement ITarget: this class also has IEmployeeDetails interface: this interface will be implemented by both of our XML & JSON Adaptees to keep similar architecture in both of the classes, so our client code won't change.
  1. using AdapterDesignPattern.Adaptees;  
  2.   
  3. namespace AdapterDesignPattern.Target  
  4. {  
  5.     public class Adapter : ITarget  
  6.     {  
  7.         public IEmployeeDetails Data;  
  8.         public Adapter(IEmployeeDetails data)  
  9.         {  
  10.             this.Data = data;  
  11.         }  
  12.         public string GetDetails()  
  13.         {  
  14.            return Data.GetData();  
  15.         }  
  16.     }  

Let's create an IEmployeeDetails interface:
  1. public interface IEmployeeDetails  
  2.   {  
  3.       string GetData();  
  4.   } 
An employee class and an entity class
  1. public class Employee  
  2.  {  
  3.      public int EmpCode { getset; }  
  4.      public string EmpName { getset; }  
  5.  } 
An adaptee class to load xml data. For now, we can generate data from code itself. 
  1. using AdapterDesignPattern.Adaptees;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Xml;  
  5. using System.Xml.Serialization;  
  6.   
  7. namespace AdapterDesignPattern  
  8. {  
  9.     public class XMLAdaptee : IEmployeeDetails  
  10.     {  
  11.         public string GetData()  
  12.         {  
  13.             List<Employee> Employees = new List<Employee>() {  
  14.                 new Employee(){  
  15.                     EmpCode = 1,  
  16.                     EmpName = "Alex"  
  17.   
  18.                 },new Employee(){  
  19.                     EmpCode = 2,  
  20.                     EmpName = "Martin"  
  21.   
  22.                 },new Employee(){  
  23.                     EmpCode = 3,  
  24.                     EmpName = "Gloria"  
  25.                 }  
  26.             };  
  27.             var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });  
  28.             var serializer = new XmlSerializer(Employees.GetType());  
  29.             var settings = new XmlWriterSettings(); settings.Indent = true;  
  30.             settings.OmitXmlDeclaration = true;  
  31.             using (var stream = new StringWriter())  
  32.             using (var writer = XmlWriter.Create(stream, settings))  
  33.             {  
  34.                 serializer.Serialize(writer, Employees, emptyNamepsaces);  
  35.                 return stream.ToString();  
  36.             }  
  37.         }  
  38.     }  

Now our JSON adaptee:
  1. using Newtonsoft.Json;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace AdapterDesignPattern.Adaptees  
  5. {  
  6.     public class JSONAdaptee : IEmployeeDetails  
  7.     {  
  8.         public string GetData()  
  9.         {  
  10.             List<Employee> Employees =  new List<Employee>() {  
  11.                 new Employee(){  
  12.                     EmpCode = 1,  
  13.                     EmpName = "Alex"  
  14.   
  15.                 },new Employee(){  
  16.                     EmpCode = 2,  
  17.                     EmpName = "Martin"  
  18.   
  19.                 },new Employee(){  
  20.                     EmpCode = 3,  
  21.                     EmpName = "Gloria"  
  22.                 }  
  23.             };  
  24.             return JsonConvert.SerializeObject(Employees);  
  25.         }  
  26.     }  

Last but not least, our caller: class program:
  1. using AdapterDesignPattern.Adaptees;  
  2. using AdapterDesignPattern.Target;  
  3. using System;  
  4.   
  5. namespace AdapterDesignPattern  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             IEmployeeDetails getXML = new XMLAdaptee();  
  12.             IEmployeeDetails getJSON = new JSONAdaptee();  
  13.   
  14.             //To get XML data  
  15.             ITarget EmployeesDetails = new Adapter(getXML);  
  16.   
  17.             //To get JSON data  
  18.             // ITarget EmployeesDetails = new Adapter(getJSON);  
  19.             Console.WriteLine(EmployeesDetails.GetDetails());  
  20.         }  
  21.     }  

Output for XML data:
 
Adapter Design Pattern
 
Output for JSON data, just uncomment the above line in the code.
 
Adapter Design Pattern
 
Perfect! I sincerely hope you enjoyed this blog and that you're inspired to apply what you've learned to your own applications. Thank you.

Happy coding.
 
Connect with me:


Similar Articles