Design Patterns (GoF) (50 Qs)•Part 1: Creational & Structural Patterns (1 - 10)
What is the Adapter Pattern and how does it reconcile incompatible interfaces?
junior LevelCode ExampleTypeScript
In Simple Words: Acts as a translator / wrapper between two incompatible interfaces:
- Analogy: A US-to-European power plug adapter.
- Example: Your legacy application expects an interface
LegacyPaymentGateway.charge(cents), but a new third-party SDK providesStripeService.createCharge(dollars). - The Adapter: Implements the legacy interface, wraps the Stripe service internally, converts cents to dollars, and forwards the call.
typescript
class StripeAdapter implements LegacyPaymentGateway {
constructor(private stripe: StripeService) {}
charge(cents: number): void {
const dollars = cents / 100;
this.stripe.createCharge(dollars);
}
}