Before we get into great detail about the factory design pattern, We begin by attempting to comprehend why we even have a design pattern, and then we gradually get to the factory.
A design pattern is a reusable solution to the recurring problem. However, a well-known group of object-oriented applications is frequently mentioned in connection with the word. Javascript is an object-oriented, prototype-based language with dynamic typing, therefore applying this collection of OO design patterns to it is not a sequential or formal process.
Creational design patterns include factory pattern.These patterns address issues with object formation, as their name would imply. The factory pattern enables us to include the process of creating an object inside a function.
Factory Pattern
Factory patterns are incredibly adaptable and provide a lot of benefits. The ability to separate object generation from implementation is its key benefit. Additionally, by employing the closure, it may be utilized to enforce encapsulation.
Decoupling Object Creation and Implementation
Javascript is well renowned for being user-friendly and straightforward. especially when generating new instances of an object. Using the new operator, we can instantly generate the instance. Let’s look at a straightforward example of creating a shirt object from any colour.
const myShirt = new Shirt(‘red’) Now lets see the creation of object using factory
function getShirtObject (color) {
return new Shirt(color)
}
const myShirt = getShirtObjectt(‘red’) The getShirtObject() factory seems to be totally unnecessary; why don’t we directly instantiate the Shirt class by using the new operator directly?
Let’s consider the scenario where the shirt class is divided into smaller groups according to colors. If we haven’t used the factory, we’ll need to edit a tonne of files, which will take a long time and be confusing. Therefore, we can quickly make adjustments as shown below with the aid of the factory.
function getShirtObject(color) {
switch(color) {
Case ‘red’:
return RedShirt()
Case ‘green’:
return GreenShirt()
Case ‘pink’:
return PinkShirt()
Case ‘yellow’:
return YellowShirt()
default:
return NoColor()
}
}We can now perceive its true value. Similar to this, employing a factory would make it very easy to switch out a library that we are currently using for another one in the future. The second-most significant usage of it can now be discussed.
Enforce encapsulation
By preventing external programmes from directly modifying some internal components details, encapsulation restricts access to such details. Through closures, encapsulation is one of the primary ways to be enforced in javascript. Enforcing private variables is easy with the help of a factory. As an example, think about
function createShirt (color) {
const colorData = {}
const shirt = {
setColor (color) {
colorData.colorName = color
},
getColor() {
Return colorData.colorName
}
}
shirt.setColor(color)
return shirt
}Above we have used closure to create two objects: a shirt object which represents the public interface returned by the factory, and a group of colorData that are inaccessible from the outside and that can be manipulated only through the interface provided by the shirt object.
Thank you for reading! I’ll be posting more design patterns that may be utilized with node.js in the future. Please share your opinions with me in the comments area and incase of any doubts, you can reach out to me here.

Akshay is a javascript enthusiast, experienced in React, Node, and Angular. Working as a Software developer in West Agile Labs. He loves solving complex problems whether it is programming or designing a system.
