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)
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.
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.
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.
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
.
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.
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.
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.
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.
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. Please get in touch with me if you have any inquiries.
Top comments (2)
Well explained!
const product1 = new User
?? You might want to rename the object. Copy-paste doesn't work everywhere :)Some comments have been hidden by the post's author - find out more