Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Adapter Pattern


An Adapter Pattern says that just “converts the interface of a class into another interface that a client wants”.

In other words, to provide the interface according to client requirement while using the services of a class with a different interface.

The Adapter Pattern is also known as Wrapper.


Advantage of Adapter Pattern

  • It allows two or more previously incompatible objects to interact.
  • It allows reusability of existing functionality.

Usage of Adapter pattern:

It is used:

  • When an object needs to utilize an existing class with an incompatible interface.
  • When you want to create a reusable class that cooperates with classes which don’t have compatible interfaces.
  • When you want to create a reusable class that cooperates with classes which don’t have compatible interfaces.

Example of Adapter Pattern

Let’s understand the example of adapter design pattern by the above UML diagram.

UML for Adapter Pattern:

There are the following specifications for the adapter pattern:

  • Target Interface: This is the desired interface class which will be used by the clients.
  • Adapter class: This class is a wrapper class which implements the desired target interface and modifies the specific request available from the Adaptee class.
  • Adaptee class: This is the class which is used by the Adapter class to reuse the existing functionality and modify them for desired use.
  • Client: This class will interact with the Adapter class.

adapteruml

Implementation of above UML:

Step 1

Create a CreditCard interface (Target interface).

  1. public interface CreditCard {
  2.     public void giveBankDetails();
  3.     public String getCreditCard();
  4. }// End of the CreditCard interface.

Step 2

Create a BankDetails class (Adaptee class).

File: BankDetails.java
  1. // This is the adapter class.
  2. public class BankDetails{
  3.     private String bankName;
  4.     private String accHolderName;
  5.     private long accNumber;
  6.     public String getBankName() {
  7.         return bankName;
  8.     }
  9.     public void setBankName(String bankName) {
  10.         this.bankName = bankName;
  11.     }
  12.     public String getAccHolderName() {
  13.         return accHolderName;
  14.     }
  15.     public void setAccHolderName(String accHolderName) {
  16.         this.accHolderName = accHolderName;
  17.     }
  18.     public long getAccNumber() {
  19.         return accNumber;
  20.     }
  21.     public void setAccNumber(long accNumber) {
  22.         this.accNumber = accNumber;
  23.     }
  24. }// End of the BankDetails class.

Step 3

Create a BankCustomer class (Adapter class).

File: BankCustomer.java
  1. // This is the adapter class
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. public class BankCustomer extends BankDetails implements CreditCard {
  5.  public void giveBankDetails(){
  6.   try{
  7.    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  8.    System.out.print(“Enter the account holder name :”);
  9.    String customername=br.readLine();
  10.    System.out.print(“\n”);
  11.    System.out.print(“Enter the account number:”);
  12.    long accno=Long.parseLong(br.readLine());
  13.    System.out.print(“\n”);
  14.    System.out.print(“Enter the bank name :”);
  15.    String bankname=br.readLine();
  16.    setAccHolderName(customername);
  17.    setAccNumber(accno);
  18.    setBankName(bankname);
  19.    }catch(Exception e){
  20.         e.printStackTrace();
  21.    }
  22.   }
  23.   @Override
  24.   public String getCreditCard() {
  25.    long accno=getAccNumber();
  26.    String accholdername=getAccHolderName();
  27.    String bname=getBankName();
  28.    return (“The Account number “+accno+” of “+accholdername+” in “+bname+ “
  29.                         bank is valid and authenticated for issuing the credit card. “);
  30.   }
  31. }//End of the BankCustomer class.

Step 4

Create a AdapterPatternDemo class (client class).

File: AdapterPatternDemo.java
  1. //This is the client class.
  2. public class AdapterPatternDemo {
  3.  public static void main(String args[]){
  4.   CreditCard targetInterface=new BankCustomer();
  5.   targetInterface.giveBankDetails();
  6.   System.out.print(targetInterface.getCreditCard());
  7.  }
  8. }//End of the BankCustomer class.

download this example

Output

  1. Enter the account holder name :Sonoo Jaiswal
  2. Enter the account number:10001
  3. Enter the bank name :State Bank of India
  4. The Account number 10001 of Sonoo Jaiswal in State Bank of India bank is valid
  5. and authenticated for issuing the credit card.

 

 

Comments

comments