Skip to content
We areTOP 1%

What is the Factory pattern, and when would you use it?

MediumConceptDesign Patterns
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

The Factory pattern moves object creation into a dedicated method or class, so client code asks for an object by type or input and receives an implementation behind a common interface. Use it when the exact class to instantiate depends on configuration or run-time data.

  • Simple factory: a static method such as NotificationFactory.create("sms") returns an SmsNotification or an EmailNotification.
  • Factory Method: a base class declares a creation method, and subclasses override it to decide which product to create.
  • Abstract Factory: creates families of related objects, such as matching buttons and menus for a light or dark theme.
  • Benefits: callers depend only on the interface, and adding a new type changes the factory rather than every caller.

More in Design Patterns

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account