Beginner LevelSoftware Engineering Principles
What is the Adapter Pattern and how does it bridge incompatible interfaces?
❓ Interview Prompt: Explain the Adapter Structural Pattern, wrapping legacy or third-party APIs, and converting incompatible interface contracts.
The Adapter Pattern acts as a wrapper between two incompatible interfaces, enabling classes to work together that otherwise couldn't due to incompatible API contracts:
Practical Example:
Adapting a third-party analytics API ('logEvent(name, payload)') to match your application's internal logging interface ('writeLog(msg)').
Code Implementation Example
typescript
// Legacy API
class LegacyPrinter { printOld(text: string) { console.log('Legacy:', text); } }
// Target Interface expected by client
interface ModernPrinter { print(text: string): void; }
// Adapter
class PrinterAdapter implements ModernPrinter {
constructor(private legacy: LegacyPrinter) {}
print(text: string) { this.legacy.printOld(text); }
}💡Bridging incompatible interfaces with the Adapter pattern.
🎯 Interview Key Takeaway
Use the Adapter pattern to wrap incompatible third-party or legacy APIs into your application's expected interface.
#Adapter#Design Patterns#Structural#Beginner
Back to all Software Engineering Principles Questions→