Design Patterns in JavaScript — Part 2: The Factory Pattern
Sachin Kasana

Sachin Kasana @sachinkasana

About: Principal Engineer by day, DevTools builder by night 🌙 | Love working with Node.js, React & AI | Sharing insights on clean code, performance, and web development. https://json-formatter-dev.vercel.ap

Location:
india
Joined:
Jun 30, 2022

Design Patterns in JavaScript — Part 2: The Factory Pattern

Publish Date: Apr 27
0 0

🏭 Design Patterns in JavaScript — Part 2: The Factory Pattern

Imagine this…

You’re building a form builder. Each field type — text, email, checkbox — needs its own logic, validation, and rendering.

Do you create separate classes and use if statements all over the place?

Or do you use a clean abstraction that gives you the right object on demand?

That’s the beauty of the Factory Pattern  — it lets you create objects without exposing the creation logic , and lets you decide the type at runtime.

Let’s break it down — no fluff, just real JavaScript examples.

🧠 Why Factory?

You use it when:

  • You need to create many types of objects
  • Their structure is mostly the same, but behavior differs
  • You don’t want to hardcode if-else chains everywhere

🛠 Real Use Case: React Form Field Generator

Let’s say you’re building dynamic form components in React:

function createFormField(type) {
  switch (type) {
    case 'text':
      return <input type="text" />;
    case 'email':
      return <input type="email" />;
    case 'checkbox':
      return <input type="checkbox" />;
    default:
      return <input />;
  }
}
Enter fullscreen mode Exit fullscreen mode

Usage:

const field = createFormField('email');
Enter fullscreen mode Exit fullscreen mode

✅ Centralized logic

✅ Easier to maintain

✅ You can even return entire components or class instances

📦 Backend Example: Dynamic API Handler

Imagine this in an Express API:

function createUserHandler(type) {
  const handlers = {
    admin: () => new AdminUser(),
    guest: () => new GuestUser(),
    default: () => new BasicUser(),
  };

  return (handlers[type] || handlers.default)();
}
Enter fullscreen mode Exit fullscreen mode

Now you don’t write:

if (type === 'admin') return new AdminUser();
Enter fullscreen mode Exit fullscreen mode

✅ Clean

✅ Extendable

✅ Predictable

💡 Advanced Use: Strategy + Factory Combo

You can use Factory to return strategies too:

function getPaymentStrategy(mode) {
  const strategies = {
    stripe: () => new StripePayment(),
    razorpay: () => new RazorpayPayment(),
    cod: () => new CashOnDelivery(),
  };

  return (strategies[mode] || strategies.cod)();
}

const payment = getPaymentStrategy('stripe');
payment.pay(); // executes Stripe logic
Enter fullscreen mode Exit fullscreen mode

🧪 Real Projects That Use Factory

real world usage of factory pattern

when to use factory design pattern

✅ Up Next: Observer Pattern

In Part 3, we’ll explore how you can use the Observer Pattern to:

  • Listen to events
  • Build state subscriptions
  • Communicate across components/services

👉 Follow the series on Medium — we’re going one pattern at a time with real code and clean examples.

Comments 0 total

    Add comment