Adapter Design Pattern
The Adapter Design Pattern is a
structural design pattern used to allow incompatible interfaces to work
together. It acts as a
bridge between two incompatible interfaces, helping legacy or third-party systems integrate smoothly without modifying
existing code.
✅ Benefits in the Banking Domain
-
๐งฉ Interoperability: Easily integrate legacy or third-party systems with your existing banking architecture.
-
๐ก️ Encapsulation: You don’t expose the legacy code or modify it directly.
-
๐ Reusability: Allows reuse of old code without rewriting.
-
๐ Scalability: Add new adapters as new services/systems are onboarded.
๐ฆ Example in the Banking Domain
✅ Use Case: Integrating a Legacy Payment Gateway with a Modern Banking App
Imagine your banking application uses a
standard interface called
PaymentProcessor
, but an old
third-party system provides a class
LegacyBankAPI
that doesn't
follow this interface.
PaymentProcessor
, but an old
third-party system provides a class
LegacyBankAPI
that doesn't
follow this interface.
๐ป
1. Target Interface (Expected by the banking app)
public interface PaymentProcessor {
void processPayment(String account, double amount);
}
๐งฑ
2. Adaptee (Legacy System with a Different Interface)
public class LegacyBankAPI {
public void makeTransaction(String accNo, double amt) {
System.out.println("Payment of ₹" + amt + " done to account " + accNo + " via LegacyBankAPI");
}
}
๐ 3. Adapter (Bridges
LegacyBankAPI
and
PaymentProcessor
)
public class LegacyBankAdapter implements PaymentProcessor {
private LegacyBankAPI legacyAPI;
public LegacyBankAdapter(LegacyBankAPI legacyAPI) {
this.legacyAPI = legacyAPI;
}
@Override
public void processPayment(String account, double amount) {
// Adapting the call
legacyAPI.makeTransaction(account, amount);
}
}
๐งช 4. Client Code (Modern banking app)
public class BankApp {
public static void main(String[] args) {
PaymentProcessor processor = new LegacyBankAdapter(new LegacyBankAPI());
processor.processPayment("1234567890", 5000.00);
}
}
Comments
Post a Comment