Author - StudySection Post Views - 674 views
Adapter Design

Adapter Design Pattern in c

The adapter design pattern is used to bridge two incompatible interfaces. It involves a single class called adapter that is responsible for communication between two independent or incompatible interfaces. It makes two incompatible interfaces work together.
It includes four components.

  • Target: It is an interface that is used by the client to achieve its request.
  • Adapter: It is a class that implements the Target interface and inherits the Adaptee class. It provides communication between the Client and Adaptee.
  • Adaptee: It is a class with the functionality required by the client. However, it does not support the client’s interface.
  • Client: It is a class that interacts with a type that implements a Target interface.

The Adapter Design patterns are often implemented in two ways.

  1. Object Adapter Design
  2. Class Adapter Design

adapter chart

creating the Employee model and a simple object into an XML converter example:
// create Employee Model:
class Employee
{
public string Emp_Name { get; set; }
public string Emp_Address { get; set; }
public int Emp_Id { get; set; }
}
// Employee details:
class EmployeeDataProvider
{
public static List GetData() => new List
{
new Employee{ Emp_Id=101,Emp_Name="Ajay",Emp_Address="Mohali"},
new Employee{ Emp_Id=102,Emp_Name="Abhi",Emp_Address="Chd"},
new Employee{ Emp_Id=103,Emp_Name="Sidh",Emp_Address="Mohali"},
new Employee{ Emp_Id=104,Emp_Name="Rajesh",Emp_Address="Chd"},
new Employee{ Emp_Id=105,Emp_Name="Ravi",Emp_Address="Mohali"}
};
}
// converter Xml
public class XmlConverter
{
public XDocument GetXML()
{
var xDocument = new XDocument();
var xElement = new XElement("Employee");
var xAtributes = EmployeeDataProvider.GetData()
.Select(c => new XElement("Employee",
new XAttribute("Employee_Id",c.Emp_Id),
new XAttribute("Employee_Name", c.Emp_Name),
new XAttribute("Employee_Address",c.Emp_Address)));
xElement.Add(xAtributes);
xDocument.Add(xElement);
Console.WriteLine(xDocument);
return xDocument;
}
}
Implement a JsonConverter class:
class JsonConverter
{
private IEnumerable _employees;
public JsonConverter(IEnumerable employees)
{
_employees = employees;
}
public void ConvertToJson()
{
var jsonemployee = JsonConvert.SerializeObject(_employees, Formatting.Indented);
Console.WriteLine("\n Print Json List\n");
Console.WriteLine(jsonemployee);
}
}
Create interface:
public interface IXmlToJson
{
void ConvertXmlToJson();
}
XmlToJsonAdapter class that will implement the IXmlToJson interface:
public class XmlToJsonAdapter: IXmlToJson
{
private readonly XmlConverter _xmlConverter;
public XmlToJsonAdapter(XmlConverter xmlConverter)
{
_xmlConverter = xmlConverter;
}
public void ConvertXmlToJson()
{
var employee = _xmlConverter.GetXML()
.Element("Employee")
.Elements("Employee")
.Select(c => new Employee
{
Emp_Id = Convert.ToInt32(c.Attribute("Employee_Id").Value),
Emp_Name = c.Attribute("Employee_Name").Value,
Emp_Address=c.Attribute("Employee_Address").Value
});
new JsonConverter(employee).ConvertToJson();
}
}
Main class:
class Program
{
static void Main(string[] args)
{
var xmlConverter = new XmlConverter();
var adapter = new XmlToJsonAdapter(xmlConverter);
adapter.ConvertXmlToJson();
}
}

If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri