{"id":4913,"date":"2024-02-18T12:12:52","date_gmt":"2024-02-18T12:12:52","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4913"},"modified":"2024-11-09T09:09:06","modified_gmt":"2024-11-09T09:09:06","slug":"adapter-pattern","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/adapter-pattern\/","title":{"rendered":"Adapter Pattern"},"content":{"rendered":"<blockquote><p>Adapter Pattern help in changing the third party integration without making much changes to the Core code.<\/p><\/blockquote>\n<p>Three main components:<\/p>\n<p><strong>Target:<\/strong> This is the interface that the client expects to use.<br \/>\n<strong>Adaptee:<\/strong> This is the existing interface that needs to be adapted to work with the client.<br \/>\n<strong>Adapter:<\/strong> This is the class that adapts the Adaptee to the Target interface.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/02\/AdapterExample.png\" alt=\"\" \/><\/p>\n<p><strong class=\"ctaHeader3\">Why to use Adapter Pattern?<\/strong><\/p>\n<ol>\n<li>It Allows Incompatible interfaces between classes to work together without modifying their source code. <\/li>\n<li>It acts as a bridge between two interfaces, making them compatible so that they can collaborate and interact seamlessly.<\/li>\n<li>This pattern is useful when integrating existing or third-party code into an application without modifying the existing codebase.<\/li>\n<\/ol>\n<p><strong class=\"ctaHeader3\">When to use Adapter Pattern?<\/strong><br \/>\nYou can use the Adapter design pattern when you have to deal with different interfaces with similar behavior (which usually means classes with similar behavior but with different methods). <\/p>\n<ol>\n<li><strong>Legacy code integration:<\/strong> When we need to integrate legacy code into a new system<\/li>\n<li><strong>Data format conversion:<\/strong> When we receive data from a third-party system, we may find that the data is in a format that is incompatible with our system.<\/li>\n<li><strong>Third-party library integration:<\/strong> When we use a third-party library in our code, we may find that the library uses an interface that is incompatible with our code.<\/li>\n<\/ol>\n<p><strong class=\"ctaHeader3\">How to implement Adapter Pattern?<\/strong><br \/>\nIn your code instead of hard coding the method calls of each vendor (or <em>Adaptee<\/em>), you could then create a generic interface to wrap these similar behaviors and work with only one type of object. The <em>Adapters<\/em> will then implement the Target interface delegating its method calls to the <em>Adaptees <\/em>that are passed to the <em>Adapters <\/em>via constructor.<\/p>\n<p><strong class=\"ctaHeader3\">Code Example:<\/strong><\/p>\n<ol>\n<li>Below we have PhonePay implementing BankAPI methods<\/li>\n<li>If Phonepay directly tries to use the classes of hdfcAPI or Any other bank api the code would become tightly coupled and would become difficult to change<\/li>\n<li>To get rid of tight coupling what we do in adaptor is we override the methods in the BankAPI and in method  definition we call respective banks which the phone pay application has tie ups<\/li>\n<li>In case there is a need tomorrow for the phonepay application to change the Bank Gateway the only place to change is where the Adapter is called.<\/li>\n<li>The <em>transferAmount<\/em> of the bank API interface is overridden in the corresponding adapter classes and the definition is changed based on the way the corresponding way Bank APIs are getting called<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/11\/Adapterpatt.png\" alt=\"\" \/><\/p>\n<p><strong>Target <\/strong>: BankAPI<br \/>\n<strong>Adapters <\/strong>: HDFCAdapter, YesAdapter, SBIAdapter<br \/>\n<strong>Adaptees <\/strong>: HDFCBankAPI, YesBankAPI, SBIBankAPI<br \/>\n<strong>Client <\/strong>: PhonePay<\/p>\n<p><strong>BankAPI.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface BankAPI {\r\n    public void transferAmount(String fromAcc, String toAcc, String amount);\r\n}\r\n<\/pre>\n<p><strong>HDFCAdapter.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class HDFCAdapter implements BankAPI {\r\n    HDFCBankAPI objHDFCBankAPI = new HDFCBankAPI();\r\n\r\n    @Override\r\n    public void transferAmount(String fromAcc, String toAcc, String amount) {\r\n        objHDFCBankAPI.performTransaction(fromAcc, toAcc, Float.valueOf(amount));\r\n    }\r\n}\r\n<\/pre>\n<p><strong>HDFCBankAPI.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class HDFCBankAPI {\r\n    public void performTransaction(String creditorAcc, String debitorAcc, Float Amount){\r\n        System.out.println(&quot;Transfering Funds&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>YesAdapter.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class YesAdapter implements BankAPI {\r\n    YesBankAPI objYesBankAPI = new YesBankAPI();\r\n\r\n    @Override\r\n    public void transferAmount(String fromAcc, String toAcc, String amount) {\r\n        objYesBankAPI.sendMoney(fromAcc, toAcc);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>YesBankAPI.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class YesBankAPI {\r\n    public boolean sendMoney(String creditAcc, String debitAcc){\r\n        System.out.println(&quot;Transaction Steps.....&quot;);\r\n        return true;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>SBIAdapter.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SBIAdapter implements BankAPI {\r\n    SBIBankAPI objSBIBankAPI = new SBIBankAPI();\r\n\r\n    @Override\r\n    public void transferAmount(String fromAcc, String toAcc, String amount) {\r\n        objSBIBankAPI.transfer(fromAcc, toAcc, amount);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>SBIBankAPI.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SBIBankAPI {\r\n    public String transfer(String account1, String account2, String Amount){\r\n        System.out.println(&quot;Transaction from Account1 to Account2&quot;);\r\n        return &quot;Transaction Successful&quot;;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>PhonePay.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class PhonePay {\r\n    public static void main(String&#x5B;] args) {\r\n        \/\/Incase you want to change the Bank Gateway tomorrow you can change the Adapter Alone\r\n        BankAPI objBankAPI = new YesAdapter();\r\n        objBankAPI.transferAmount(&quot;AccNo 154264&quot;, &quot;AccNo 4264755&quot;, &quot;452&quot;);\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Adapter Pattern help in changing the third party integration without making much changes to the Core code. Three main components: Target: This is the interface that the client expects to use. Adaptee: This is the existing interface that needs to be adapted to work with the client. Adapter: This is the class that adapts the&hellip; <a href=\"https:\/\/codethataint.com\/blog\/adapter-pattern\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[355,356],"tags":[],"class_list":["post-4913","post","type-post","status-publish","format-standard","hentry","category-structural-pattern","category-adapter-pattern"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4913"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4913\/revisions"}],"predecessor-version":[{"id":5329,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4913\/revisions\/5329"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}