Adapter Design Pattern

Adapter Design Pattern

Adapter design pattern is one of the structural design pattern. Adapter Design Pattern is used to two unrelated interfaces can work together. The object that joins these unrelated interface is called an Adapter. As a real life example, we can think of a mobile charger as an adapter because mobile battery needs 3 volts to charge but the normal socket produces either 120V (US) or 240V (India). So the mobile charger works as an adapter between mobile charging socket and the wall socket.

An adapter pattern is also known as Wrapper pattern as well. Adapter Design is very useful for the system integration when some other existing components have to be adapted by the existing system without source code modifications.

Example

Create a DebitCard interface (Target interface)

package com.ashok.designpatterns.adapter;

/**
 * 
 * @author ashok.mariyala
 *
 */
public interface DebitCard {
   public void giveBankDetails();

   public String getDebitCard();
}

BankDetails class (Adaptee class)

package com.ashok.designpatterns.adapter;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class BankDetails {
   private String bankName;
   private String accHolderName;
   private long accNumber;

   public String getBankName() {
      return bankName;
   }

   public void setBankName(String bankName) {
      this.bankName = bankName;
   }

   public String getAccHolderName() {
      return accHolderName;
   }

   public void setAccHolderName(String accHolderName) {
      this.accHolderName = accHolderName;
   }

   public long getAccNumber() {
      return accNumber;
   }

   public void setAccNumber(long accNumber) {
      this.accNumber = accNumber;
   }
}

BankCustomer class (Adapter class)

package com.ashok.designpatterns.adapter;

import java.util.Scanner;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class BankCustomer extends BankDetails implements DebitCard {
   Scanner scan = null;
   public void giveBankDetails() {
      try {
         scan = new Scanner(System.in);

         System.out.print("Enter Account Holder Name : ");
         String customername = scan.next();

         System.out.print("Enter Account Number: ");
         long accno = scan.nextLong();

         System.out.print("Enter Bank Name : ");
         String bankname = scan.next();

         setAccHolderName(customername);
         setAccNumber(accno);
         setBankName(bankname);
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         if(null != scan) {
            scan.close();
         }
      }
   }

   @Override
   public String getDebitCard() {
      return ("Successfully authenticated for issuing the Debit card. ");
   }
}

TestAdapterPattern class (client class)

package com.ashok.designpatterns.adapter;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class TestAdapterPattern {
 public static void main(String args[]) {
  DebitCard targetInterface = new BankCustomer();
  targetInterface.giveBankDetails();
  System.out.print(targetInterface.getDebitCard());
 }
}

Output
Enter Account Holder Name : M V Ashok Kumar
Enter Account Number: 10256897554
Enter Bank Name : ICICI
Successfully authenticated for issuing the Debit card. 
Adapter Design Pattern
Scroll to top