Factory Design Pattern in JavaScript
Tags: javascript, productivity, designpattern, factorypattern
Published: 2022-09-17 18:56:35

# Design Pattern

Design patterns are models or plans for resolving typical issues in software development. They are the best practises that programmers can adhere to when creating software applications.

There are numerous varieties of design patterns. 3 main categories of design patterns are:

- Structural patterns
- Behavioral patterns
- Creational patterns (Factory Pattern falls under this category)

<br />
<br />

# Introduction on Factory Pattern

We can generate multiple of the same objects using the Factory Pattern by using a unique function called the factory function.
A general interface for creating objects is offered by the factory design pattern, which is a creational design pattern. 

We do not have to explicitly require a constructor function when using the factory pattern as we can indicate the type of object that is being constructed.

In the functional programming paradigm, a factory is an entity that produces objects, such as a class or function.

<br />
<br />

# In-depth explanation


Factory Pattern is not indeed a pattern. The factory pattern in JavaScript essentially consists of a function that returns an object without utilizing the new keyword. 

We can build quick factory functions that always return an object thanks to ES6's arrow functions.

In many cases, to create new instances, it may often be more memory-efficient instead of creating new objects every time. 

Let's take the example of creating **Product** and implement it by creating new instances.



![classBased](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxjbhbez8o5hjat8y8v7.png)

<br />

Factory Pattern follows **Object Oriented Programming**, Design Pattern which involves creating objects.

A **constructor** function for various object types is wrapped by the factory pattern, which then returns instances of the objects via a simple API. It facilitates the creation of various objects that returns the desired object type.

Despite the fact that **JavaScript** offers a variety of object generation techniques, the factory pattern helps us to keep our object creation code streamlined and reusable.

<br />
<br />

## Implementation

Let’s start by building our own function which will be responsible for returning new objects of a specific type whenever called.

In the `factory` folder, let’s create a file named `product.js`.


![factoryPattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtvnqb9e12j5wgbcasqq.png)


In this file, we build a `createProduct` constructor function. It accepts an object as a parameter attributes for initializing the object with various info i.e name, description, brand, size, color, createdAt timestamp. 
Then we can create multiple objects with the same properties and different values of those.

<br />
<br />

## When to use a factory pattern

  - If a class is unable to identify the subclass, it must construct one.

  - When we need to generate several objects with the same properties without having to duplicate the same code.

  - Whenever the process of creating an object is particularly complex

  - When we need to return a custom object based on the existing environment, or when a user-specific configuration is required.

  - When you don't know in advance the precise types and dependencies of the objects your code needs to work with, use the factory method.

<br />
<br />

## Advantage of using factory pattern

  - Encourage code reusability and maintainability

  - Allowing you more freedom to develop different types of objects that use the same API.

  - Helps to promote loose decoupling by isolating the construction of the object from its implementation.

  - Helps in memory conservation.

<br />
<br />

## Disadvantage of using factory pattern

  - In some circumstances, implementation can be challenging.

  - Due to the level of abstraction it introduces, testing might be challenging depending on complexity.

<br />
<br />

# Conclusion

Use the factory method when you are unsure of the exact types and dependencies of the objects your code will be working with in advance.

When you want to conserve system resources by reusing existing objects rather than rebuilding them each time, use the factory method. When working with large, resource-intensive objects like database connections, file systems, and network resources, you frequently run into this requirement. 

This pattern not only helps in code reusability and maintainability but also preserve memory and you have more control over objects.

 If you have any queries, then feel free to connect with me on my LinkedIn profile - [Shubham Dutta](https://www.linkedin.com/in/shubham--dutta/). Please get in touch with me if you have any inquiries.