Loading...
Learn Factory Pattern — a key concept for LLD interviews.
Factory Pattern creates objects without the client knowing the creation logic. Client says "give me a Circle", factory handles the "new Circle(radius, color, ...)". Useful when object creation is complex or varies by type.
You're building a notification system for an e-commerce platform. Right now it only sends emails. But the product team wants SMS for order updates and push notifications for promotions. The current code has if-else chains everywhere to decide which notification to send.
function sendNotification(type: string, to: string, msg: string) {
if (type === 'email') {
const smtp = new SmtpClient()
smtp.connect('smtp.gmail.com')
smtp.send({ to, subject: 'Notification', body: msg })
smtp.disconnect()
} else if (type === 'sms') {
const twilio = new TwilioClient(API_KEY)
twilio.messages.create({ to, body: msg })
} else if (type === 'push') {
const firebase = new FirebaseAdmin()
firebase.messaging().send({ token: to, body: msg })
}
// TODO: Slack, WhatsApp, in-app...
}What's the core problem with this approach?