{"id":6221,"date":"2022-11-21T04:37:52","date_gmt":"2022-11-21T04:37:52","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=6221"},"modified":"2022-11-21T06:45:50","modified_gmt":"2022-11-21T06:45:50","slug":"adapter-design-pattern-in-python","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/","title":{"rendered":"Adapter Design Pattern in Python"},"content":{"rendered":"<p>Many times code for classes was written, and we didn&#8217;t have any option to modify the code or change the interface according to needs. This generally happens when the method we are calling and the method from which we are calling are on different systems.<\/p>\n<p>So here come the Adapters, In simple words, an Adapter is a design pattern that connects two incompatible interfaces or systems. This type of design pattern is also referred to as a structural pattern.<\/p>\n<h2>Adapters solve problems like:<\/h2>\n<ul>\n<li>How can a class be reused that does not have an interface that is required?<\/li>\n<li>How can two incompatible classes or systems work together?<\/li>\n<li>How can an alternative interface or system be provided to a class?<\/li>\n<\/ul>\n<p>Adapters provide a single class that can combine two independent interfaces. A real-life example of an adapter is an earphone. Earphones are able to take audio from a phone or tablet and put it in our ears. You can plug the earphone jack into the phone and put earphones in your ears, so you can hear audio.<\/p>\n<p>In two objects Adapter acts as a wrapper. The adapter catches calls from one object or function and changes them according to the requirements of another object.<\/p>\n<p><strong>Basically, two types of adapter patterns exist:<\/strong><\/p>\n<ul>\n<li>Object Adapter Pattern<\/li>\n<li>Class Adapter Pattern<\/li>\n<\/ul>\n<p><em><strong>Object Adapter Pattern:<\/strong><\/em> It works on object implementation.<br \/>\n<em><strong>Class Adapter Pattern:<\/strong><\/em> It works using multiple inheritances. It can also refer to an alternative method to implement the adapter pattern.<\/p>\n<p>Adapters are very commonly used in Python. Adapters generally work in systems where old computer source code is used. Adapters are able to convert these legacy code systems to modern code and let them interact with each other.<\/p>\n<p>Note: Adapters are also a kind of <a href=\"https:\/\/studysection.com\/blog\/decorators-with-python\/\">decorator<\/a> that can change the functionality of a function or object.<\/p>\n<p><strong>Adapter patterns are generally composed of:<\/strong><\/p>\n<ul>\n<li>Which classes do adapters have?<\/li>\n<li>What functionalities do those classes have?<\/li>\n<li>How are patterns related?<\/li>\n<\/ul>\n<h3>Example<\/h3>\n<p><code>class aim:<br \/>\ndef request(self) -&gt; str:<br \/>\nreturn \"aim: default aim\u2019s behavior.\"<br \/>\nclass accepter:<br \/>\ndef specific_request(self) -&gt; str:<br \/>\nreturn \"accepter: accepter.\"<br \/>\nclass  accept:<br \/>\ndef __init__(self, accepter: Accepter) -&gt; None:<br \/>\nself.accepter = accepter<br \/>\ndef request(self) -&gt; str:<br \/>\nreturn f\"accept: (TRANSLATED) {self.accepter.specific_request()[::-1]}\"<br \/>\ndef client(aim: Target) -&gt; None:<br \/>\nprint(aim.request(), end=\"\")<br \/>\nif __name__ == \"__main__\":<br \/>\nprint(\"client: interface of aim object work:\")<br \/>\naim = aim()<br \/>\nclient_code(aim)<br \/>\nprint(\"\\n\")<br \/>\naccepter = accepter()<br \/>\nprint(\"client: accepter class has a weird interface. See, I don't understand it:\")<br \/>\nprint(f\"accepter: {accepter.specific_request()}\", end=\"\\n\\n\")<br \/>\nprint(\"client: using accepter things can work:\")<br \/>\nadapter = accept(accepter)<br \/>\nclient_code(adapter)<\/code><\/p>\n<p><strong><em>Output<\/em><\/strong><br \/>\n<em>client:<\/em> interface of aim object work:<br \/>\naim: The default target&#8217;s behavior.<br \/>\n<em>client:<\/em> accepter class has a weird interface. See, I don&#8217;t understand it:<br \/>\naccepter: accepter.<br \/>\n<em>client:<\/em> using accepter things can work:<br \/>\n<em>accept:<\/em> (TRANSLATED) Special behavior of the accepter.<\/p>\n<p>The code includes an <strong>accept <\/strong>interface with various parameters and attributes. It includes an <strong>accepter <\/strong>along with an <strong>aim <\/strong>interface that implements all the attributes and displays the output as visible.<\/p>\n<h3>Diagram<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/11\/accepter-digram.png\" alt=\"accepter-digram\" \/><\/p>\n<h3>Summary<\/h3>\n<ul>\n<li>You can use an adapter if you want to use an existing class, but its interface is not what you want.<\/li>\n<li>The adapter modifies the interface of its parent class for situations when it is not possible to modify the parent class that needs to interact with another class.<\/li>\n<li>An adapter can provide an alternative interface to an existing object without actually changing it.<\/li>\n<li>Adapter act as decorators for existing objects and classes.<\/li>\n<li>An adapter can also be referred to as a bridge that acts as a connector between two interfaces and objects or systems.<\/li>\n<\/ul>\n<p><small><em>StudySection gives an opportunity to beginners and experts in .NET framework to go through StudySection\u2019s <a href=\"https:\/\/www.studysection.com\/vb-net-web-developer-advanced-diploma\">.NET Certification Exam<\/a> and get a .NET certification for enhancement of career in programming. If you have knowledge of the .NET framework then you can get a certificate through an online exam at StudySection.<\/em><br \/>\n<\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many times code for classes was written, and we didn&#8217;t have any option to modify the code or change the<\/p>\n","protected":false},"author":1,"featured_media":6222,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[818,33],"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 Python - StudySection Blog<\/title>\n<meta name=\"description\" content=\"The adapter design pattern provides a single class that can combine two independent interfaces. Its real-time example is earphones.\" \/>\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-python\/\" \/>\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 Python - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"The adapter design pattern provides a single class that can combine two independent interfaces. Its real-time example is earphones.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/\" \/>\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-11-21T04:37:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T06:45:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/11\/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=\"3 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-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Adapter Design Pattern in Python\",\"datePublished\":\"2022-11-21T04:37:52+00:00\",\"dateModified\":\"2022-11-21T06:45:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/\"},\"wordCount\":545,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Adapter\",\"Python\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/\",\"url\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/\",\"name\":\"Adapter Design Pattern in Python - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2022-11-21T04:37:52+00:00\",\"dateModified\":\"2022-11-21T06:45:50+00:00\",\"description\":\"The adapter design pattern provides a single class that can combine two independent interfaces. Its real-time example is earphones.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Adapter Design Pattern in Python\"}]},{\"@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 Python - StudySection Blog","description":"The adapter design pattern provides a single class that can combine two independent interfaces. Its real-time example is earphones.","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-python\/","og_locale":"en_US","og_type":"article","og_title":"Adapter Design Pattern in Python - StudySection Blog","og_description":"The adapter design pattern provides a single class that can combine two independent interfaces. Its real-time example is earphones.","og_url":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2022-11-21T04:37:52+00:00","article_modified_time":"2022-11-21T06:45:50+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/11\/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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Adapter Design Pattern in Python","datePublished":"2022-11-21T04:37:52+00:00","dateModified":"2022-11-21T06:45:50+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/"},"wordCount":545,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Adapter","Python"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/","url":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/","name":"Adapter Design Pattern in Python - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2022-11-21T04:37:52+00:00","dateModified":"2022-11-21T06:45:50+00:00","description":"The adapter design pattern provides a single class that can combine two independent interfaces. Its real-time example is earphones.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/adapter-design-pattern-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Adapter Design Pattern in Python"}]},{"@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":324,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6221"}],"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=6221"}],"version-history":[{"count":2,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6221\/revisions"}],"predecessor-version":[{"id":6225,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6221\/revisions\/6225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/6222"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=6221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=6221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=6221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}