Readers like you help support MUO. If you make a purchase through links on our site, we may receive an affiliate commission. Continue reading.
With the advent of service-oriented architectures (SOA), more and more organizations are using third-party services for their enterprise systems. These companies must keep up with a rapidly changing business environment, which may mean abandoning the services of one service provider in favor of another.
In such cases, a company’s existing architecture may not be compatible with that of a new vendor. Instead of wasting months rewriting corporate code to make it compatible with new software, a company can use the adapter design pattern.
What is the adapter design pattern?
The adapter pattern allows classes with incompatible interfaces to work together. It does this by transforming the interface of an existing class (or software) into one that a client (or service) expects. There are two types of adapters: object adapters and class adapters.
The object adapter uses composition to package the adaptee with the adapter, effectively creating the interface that the client expects. So if the client expects a string, the adapter takes an integer (the adaptee) and gives it the properties of a string.
The class diagram above represents the object adapter. the class adapter implements the target interfaceto effectively gain access to all methods of the interface. Then it adapts adapted and wrap that up adapted with the target interface methods.
The class adapter uses multiple inheritance, with the adapter class being the subclass of both the adaptee and target classes. The following class diagram represents the class adapter that you can use in programming languages that support multiple inheritance.
Implementing the adapter design pattern in Java
This application implements the object adapter. This sample application simulates a financial organization transitioning from using and processing charge cards to credit cards. This organization originally used the following charge card interface:
public interface ChargeCard {
public void monthlyBalance();
public void lateFee();
public void Annualfee();
}
A popular type of charge card that this organization’s system processes is the plum card:
public class PlumCard implements ChargeCard {
private int cardNo;
private String customerName;
private double balance;
// primary constructor
public PlumCard(int cardNo, String customerName, double balance) {
this.cardNo = cardNo;
this.customerName = customerName;
this.balance = balance;
}
// getters and setters
public int getCardNo() {
return cardNo;
}
public void setCardNo(int cardNo) {
this.cardNo = cardNo;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public void monthlyBalance() {
System.out.println("In January " + this.customerName + " spent " + this.balance);
}
@Override
public void lateFee() {
System.out.println(this.customerName + " monthly latefee is $80.00");
}
@Override
public void Annualfee() {
System.out.println(this.customerName + " annual fee is $200.00");
}
}
This financial institution is switching to credit cards and issuing the charge cards, so some of their customers now have credit cards:
public interface CreditCard {
public void monthlyMinPayment();
public void interest();
public void Annualfee();
}
The financial institutions’ corporate system now only processes credit cards as they intend to phase out the use of charge cards in the coming year. But most of their customers still use charge cards. So the engineers decided it was best to implement an adapter until all of their customers had switched to a credit card.
public class ChargeCardAdapter implements CreditCard {
ChargeCard chargeCard;
public ChargeCardAdapter(ChargeCard chargeCard) {
this.chargeCard = chargeCard;
}
@Override
public void monthlyMinPayment() {
this.chargeCard.monthlyBalance();
}
@Override
public void interest() {
this.chargeCard.lateFee();
}
@Override
public void Annualfee() {
this.chargeCard.Annualfee();
}
}
This ChargeCardAdapter Java class implements the CreditCard Java interface. It adapts ChargeCard (the adapted one) and gives it the properties of a credit card. For example, the system processes a credit card’s interest charge, which is the late charge of a charge card customer who has not yet converted.
Benefits of using the adapter pattern
The main benefit of using the adapter pattern is that clients can use new services, libraries, and functionality without changing their code. This promotes the scalability of the application.
Another advantage is that this pattern is also flexible. The adapter pattern provides two implementation methods: object adapter and class adapter.
This article was previously published on Source link