Skip to main content
InterviewHubPRO
Section Overview
Q3

Question 3 of 50

Part 1: Creational & Structural Patterns (1 - 10)

6%
Design Patterns (GoF) (50 Qs)•Part 1: Creational & Structural Patterns (1 - 10)

What is the Factory Method Pattern?

junior LevelCode Exampledesign-patternsCreational

In Simple Words: Defines an interface for creating an object, but lets subclasses decide which class to instantiate:

  • Decouples client code from concrete implementation classes:
typescript
interface Notification {
  send(message: string): void;
}

class EmailNotification implements Notification {
  send(msg: string) { console.log(`Sending Email: ${msg}`); }
}

class SMSNotification implements Notification {
  send(msg: string) { console.log(`Sending SMS: ${msg}`); }
}

class NotificationFactory {
  static create(type: "email" | "sms"): Notification {
    if (type === "email") return new EmailNotification();
    if (type === "sms") return new SMSNotification();
    throw new Error("Unknown notification type");
  }
}