Archive for category Adapter Patterns

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.

Example:

package adapter;

public interface CreditCard {
public void initBankInfo();

public String getCreditCardInfo();

}

package adapter;

public class Bank {

private String bankName;
private String accHolderName;
private String 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 String getAccNumber() {
return accNumber;
}

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

}

package adapter;

public class BankCustomer extends Bank implements CreditCard {

@Override
public void initBankInfo() {

setBankName(“CityBank”);
setAccNumber(“2125-57891-AB1”);
setAccHolderName(“Sezayir Dagtekin”);

}

@Override
public String getCreditCardInfo() {

return “Account Number:” + getAccNumber() + ” Account Holder Name:” + getAccHolderName() + ” Bank Name:”+ getBankName();

}

}

package adapter;

public class AdapterPatternDemo {
public static void main(String args[]) {
CreditCard adapter = new BankCustomer();
adapter.initBankInfo();
String info = adapter.getCreditCardInfo();
System.out.println(info);

}

}

Leave a comment