Interview frequency: …
Short answer
Creational patterns manage object creation; Structural patterns organize relationships between classes; and Behavioral patterns organize communication and distribute responsibilities.
Explanation
Design patterns are tried-and-tested, documented solutions to common engineering problems. Creational patterns (such as Factory and Singleton) separate object creation from use. Structural patterns (such as Adapter and Decorator) show how to combine objects into larger, more flexible structures. Behavioral patterns (such as Strategy and Observer) define how data and messages flow and how responsibilities are distributed among objects.
Code example
// Creational — Factory: callers do not know which class gets created
function createLogger(env: string): Logger {
return env === "prod" ? new CloudLogger() : new ConsoleLogger()
}
// Structural — Adapter: make an incompatible interface fit the one we expect
class LegacyPaymentAdapter implements PaymentGateway {
constructor(private legacy: OldGateway) {}
pay(amount: number) { return this.legacy.makePayment(Math.round(amount * 100)) }
}
// Behavioral — Strategy: swap an algorithm at runtime
class Checkout {
constructor(private shipping: (weightKg: number) => number) {}
total(weightKg: number) { return this.shipping(weightKg) }
}
new Checkout((w) => w * 2).total(3) // 6Common mistakes
- Treating design patterns as ready-made code templates to copy verbatim instead of understanding the architectural problem they solve.