{"id":5622,"date":"2022-03-02T04:41:33","date_gmt":"2022-03-02T04:41:33","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=5622"},"modified":"2022-03-02T05:50:20","modified_gmt":"2022-03-02T05:50:20","slug":"adapter-design-pattern-in-c","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/","title":{"rendered":"Adapter Design Pattern in c"},"content":{"rendered":"<p>The adapter <a href=\"https:\/\/studysection.com\/blog\/state-design-pattern\/\">design pattern<\/a>  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.<br \/>\nIt includes four components.<\/p>\n<ul>\n<li><strong>Target:<\/strong> It is an interface that is used by the client to achieve its request.<\/li>\n<li><strong>Adapter:<\/strong> It is a class that implements the Target interface and inherits the Adaptee class. It provides communication between the Client and Adaptee.<\/li>\n<li><strong>Adaptee:<\/strong> It is a class with the functionality required by the client. However, it does not support the client&#8217;s interface.<\/li>\n<li><strong>Client:<\/strong> It is a class that interacts with a type that implements a Target interface.<\/li>\n<\/ul>\n<p>The Adapter Design patterns are often implemented in two ways.<\/p>\n<ol>\n<li>Object Adapter Design<\/li>\n<li>Class Adapter Design<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/03\/adapter-chart.png\" alt=\"adapter chart\"\/><\/p>\n<p>creating the Employee model and a simple object into an XML converter example:<br \/>\n<code><strong>\/\/ create Employee Model:<\/strong><br \/>\nclass Employee<br \/>\n    {<br \/>\n        public string Emp_Name { get; set; }<br \/>\n        public string Emp_Address { get; set; }<br \/>\n        public int Emp_Id { get; set; }<br \/>\n    }<br \/>\n\/\/ Employee details:<br \/>\n class EmployeeDataProvider<br \/>\n  {<br \/>\n      public static List<Employee> GetData() => new List<Employee><br \/>\n        {<br \/>\n           new Employee{ Emp_Id=101,Emp_Name=\"Ajay\",Emp_Address=\"Mohali\"},<br \/>\n            new Employee{ Emp_Id=102,Emp_Name=\"Abhi\",Emp_Address=\"Chd\"},<br \/>\n            new Employee{ Emp_Id=103,Emp_Name=\"Sidh\",Emp_Address=\"Mohali\"},<br \/>\n            new Employee{ Emp_Id=104,Emp_Name=\"Rajesh\",Emp_Address=\"Chd\"},<br \/>\n            new Employee{ Emp_Id=105,Emp_Name=\"Ravi\",Emp_Address=\"Mohali\"}<br \/>\n            };<br \/>\n    }<br \/>\n\/\/ converter  Xml<br \/>\npublic class XmlConverter<br \/>\n    {<br \/>\n        public XDocument GetXML()<br \/>\n        {<br \/>\n            var xDocument = new XDocument();<br \/>\n            var xElement = new XElement(\"Employee\");<br \/>\n            var xAtributes = EmployeeDataProvider.GetData()<br \/>\n            .Select(c => new XElement(\"Employee\",<br \/>\n            new XAttribute(\"Employee_Id\",c.Emp_Id),<br \/>\n            new XAttribute(\"Employee_Name\", c.Emp_Name),<br \/>\n            new XAttribute(\"Employee_Address\",c.Emp_Address)));<br \/>\n            xElement.Add(xAtributes);<br \/>\n            xDocument.Add(xElement);<br \/>\n            Console.WriteLine(xDocument);<br \/>\n            return xDocument;<br \/>\n        }<br \/>\n}<br \/>\nImplement a JsonConverter class:<br \/>\n  class JsonConverter<br \/>\n    {<br \/>\n        private IEnumerable<Employee> _employees;<br \/>\n        public JsonConverter(IEnumerable<Employee> employees)<br \/>\n        {<br \/>\n     \t       _employees = employees;<br \/>\n        }<br \/>\n        public void ConvertToJson()<br \/>\n        {<br \/>\nvar jsonemployee = JsonConvert.SerializeObject(_employees, Formatting.Indented);<br \/>\n           Console.WriteLine(\"\\n Print Json List\\n\");<br \/>\n           Console.WriteLine(jsonemployee);<br \/>\n        }<br \/>\n    }<br \/>\nCreate interface:<br \/>\npublic interface IXmlToJson<br \/>\n {<br \/>\n        void ConvertXmlToJson();<br \/>\n }<br \/>\nXmlToJsonAdapter class that will implement the IXmlToJson interface:<br \/>\npublic class XmlToJsonAdapter: IXmlToJson<br \/>\n {<br \/>\n        private readonly XmlConverter  _xmlConverter;<br \/>\n        public XmlToJsonAdapter(XmlConverter xmlConverter)<br \/>\n        {<br \/>\n           \t_xmlConverter = xmlConverter;<br \/>\n         }<br \/>\n        public void ConvertXmlToJson()<br \/>\n        {<br \/>\n            var employee = _xmlConverter.GetXML()<br \/>\n         \t\t\t       .Element(\"Employee\")<br \/>\n               \t\t      .Elements(\"Employee\")<br \/>\n                \t\t      .Select(c => new Employee<br \/>\n                \t\t\t{<br \/>\n                    \t\t\t\tEmp_Id = Convert.ToInt32(c.Attribute(\"Employee_Id\").Value),<br \/>\n                   \t\t\t\tEmp_Name = c.Attribute(\"Employee_Name\").Value,<br \/>\n                  \t\t\t\tEmp_Address=c.Attribute(\"Employee_Address\").Value<br \/>\n});<br \/>\n new JsonConverter(employee).ConvertToJson();<br \/>\n}<br \/>\n      }<br \/>\nMain class:<br \/>\nclass Program<br \/>\n    {<br \/>\n        static void Main(string[] args)<br \/>\n        {<br \/>\n          var xmlConverter = new XmlConverter();<br \/>\n        \t          var adapter = new XmlToJsonAdapter(xmlConverter);<br \/>\n         \t          adapter.ConvertXmlToJson();<br \/>\n        }<br \/>\n    }<\/code><\/p>\n<p><small>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 <a href=\"https:\/\/www.studysection.com\/php-web-developer-advanced-diploma\">PHP Certification Exams<\/a> are offered by StudySection along with other programming certification exams. <\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The adapter design pattern is used to bridge two incompatible interfaces. It involves a single class called adapter that is<\/p>\n","protected":false},"author":1,"featured_media":5624,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[142,757],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Adapter Design Pattern in c - StudySection Blog<\/title>\n<meta name=\"description\" content=\"The adapter design pattern is used to bridge two incompatible interfaces. It involves a single class called adapter that is responsible\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Adapter Design Pattern in c - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"The adapter design pattern is used to bridge two incompatible interfaces. It involves a single class called adapter that is responsible\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-02T04:41:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-02T05:50:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/03\/Adapter-Design.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Adapter Design Pattern in c\",\"datePublished\":\"2022-03-02T04:41:33+00:00\",\"dateModified\":\"2022-03-02T05:50:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/\"},\"wordCount\":195,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"C\",\"Design\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/\",\"url\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/\",\"name\":\"Adapter Design Pattern in c - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2022-03-02T04:41:33+00:00\",\"dateModified\":\"2022-03-02T05:50:20+00:00\",\"description\":\"The adapter design pattern is used to bridge two incompatible interfaces. It involves a single class called adapter that is responsible\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Adapter Design Pattern in c\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Adapter Design Pattern in c - StudySection Blog","description":"The adapter design pattern is used to bridge two incompatible interfaces. It involves a single class called adapter that is responsible","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Adapter Design Pattern in c - StudySection Blog","og_description":"The adapter design pattern is used to bridge two incompatible interfaces. It involves a single class called adapter that is responsible","og_url":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2022-03-02T04:41:33+00:00","article_modified_time":"2022-03-02T05:50:20+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/03\/Adapter-Design.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Adapter Design Pattern in c","datePublished":"2022-03-02T04:41:33+00:00","dateModified":"2022-03-02T05:50:20+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/"},"wordCount":195,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["C","Design"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/","url":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/","name":"Adapter Design Pattern in c - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2022-03-02T04:41:33+00:00","dateModified":"2022-03-02T05:50:20+00:00","description":"The adapter design pattern is used to bridge two incompatible interfaces. It involves a single class called adapter that is responsible","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Adapter Design Pattern in c"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":789,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5622"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=5622"}],"version-history":[{"count":3,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5622\/revisions"}],"predecessor-version":[{"id":5628,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5622\/revisions\/5628"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/5624"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=5622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=5622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=5622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}