Skip to content

Factory Pattern in JavaScript

The Factory Pattern allows us to create objects using factory functions, without the need for the new keyword. This pattern is useful when creating multiple instances of objects with similar properties or when object creation involves complex configuration.

Table of Contents

Implementation

In JavaScript, a factory function is a function that returns a new object. Here’s an example of a factory function createUser that creates user objects:

javascript
const createUser = ({ firstName, lastName, email }) => ({
  firstName,
  lastName,
  email,
  fullName() {
    return `${this.firstName} ${this.lastName}`;
  },
});

// Usage
const user1 = createUser({
  firstName: "John",
  lastName: "Doe",
  email: "john@doe.com",
});

const user2 = createUser({
  firstName: "Jane",
  lastName: "Doe",
  email: "jane@doe.com",
});

console.log(user1.fullName()); // Output: John Doe
console.log(user2.fullName()); // Output: Jane Doe

Use Cases

The Factory Pattern is useful in the following scenarios:

  1. Creating Multiple Objects: When we need to create multiple objects with similar properties.
  2. Complex Configuration: When object creation involves complex setup or configuration that can be encapsulated in a factory function.

Pros and Cons

Pros:

  • Flexibility: Easily create objects with custom configurations.
  • Encapsulation: Encapsulate object creation logic within a function.
  • Reusable: The factory function can be reused to create multiple objects.

Cons:

  • Memory Efficiency: May create new instances instead of reusing objects.
  • Less Explicit: In some cases, using classes may provide clearer code organization.

Conclusion

The Factory Pattern in JavaScript is a powerful tool for creating objects with customizable configurations. It promotes code reuse and encapsulation, making it easier to manage object creation logic. While it may not always be the most memory-efficient choice, it provides flexibility and can simplify object creation in many scenarios.

Made By Sourav Bandyopadhyay