LogoLogo

Design Patterns - Factory Pattern

Prasun Das| May 29, 2023 at 9:57 AM | 3 minutes Read

Brief Introduction of Design Pattern


Models or strategies for tackling frequent problems in software development are known as design patterns. They are the finest procedures for programmers to follow when developing software programmes.


Design patterns come in a wide variety. There are three primary types of design patterns:


  1. Behavioral patterns
  2. Structural patterns
  3. Creational patterns


Factory Pattern falls under the Creational pattern. Now let's understand what factory pattern is.


Overview of Factory Pattern


Utilizing the Factory Pattern, we can create numerous copies of the same object by using a special function called the factory function.

The factory design pattern, a creational design pattern, provides a common interface for creating objects.


By indicating the sort of object that is being produced, we may use the factory pattern without explicitly requiring a constructor function.


A factory in the functional programming paradigm is a thing that creates things, like classes or functions.


Detailed explanation of Factory Pattern


In actuality, Factory Pattern is not a pattern. In JavaScript, a function that returns an object without using the new keyword constitutes the factory pattern.


The factory pattern helps us keep our object creation code simple and reusable even though JavaScript provides a range of object production methods.


Thanks to ES6's arrow functions, we can quickly create factory functions that always return an object.


Instead of producing new objects repeatedly, it may frequently be more memory-efficient to create new instances.


Let's use the creation of a product as an example and implement it by producing new instances.




Implementation


The Factory Pattern is a Design Pattern used in Object Oriented Programming, which includes producing objects.


Let's begin by creating our own function, which when called will always return new objects of a particular kind.Let's create a file called employee and place it in the factory folder.




We construct a createEmployee constructor function in this file. It accepts an object as a parameter and initializes the object with various information, such as empName, empEmail, empDepartment, empSalary, empAddress, and createdAt date.


Then, using alternative values for those characteristics, we can build several objects with the same properties.




When should a factory pattern be used?


1.A class must create a subclass if it is unable to recognise an existing one.


2.When it is necessary to create several objects with the same properties without using the same code twice.


3.While making a product involves an extremely difficult process


4.When a user-specific configuration is required or when it is necessary to return a custom object based on the current environment.


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


Pros and Cons of Factory Pattern


1. Pros:

  1. Encourage maintainability and code reuse.
  2. By separating the object's construction from its use, it aids in loose decoupling.
  3. Aids in maintaining memory.


2. Cons:

  1. Implementation may be difficult in some cases.
  2. Testing may be difficult due to the level of abstraction it introduces, depending on complexity.



Conclusion


Use the factory technique to reuse existing objects rather than constructing them each time to save system resources. You typically encounter this requirement while working with large, resource-intensive objects like database connections, file systems, and network resources.


This pattern aids in object control and memory preservation in addition to code reuse and maintainability.


Feel free to connect with me on LinkedIn by searching for Shubham Dutta if you have any questions. Please contact me if you have any questions.



#design-patterns#system-design
View Count:4.2k
1

Comments

Similar Articles