Factory Design Pattern Can Help !

While writing code, do you worry about repeated code, structuring and maintenance then I believe factory design patterns can help you.

 

Before understanding the factory design pattern, let’s understand what factory means. It’s a place where something is produced right!
Now let’s understand a little deeper, we see different types of factories around us which produce different types of products. Likewise, let’s take an example of a toy factory, which produces items like iron man and Thor. So I want bulk order of iron man. I can ask for it specifically. If I want to bulk order mixed toys, I only ask for one factory that produces them.

 

Similarly, we can create a factory in programming using a factory design pattern. In programming, instead of toys, we have objects. We can build and categorise object creation using factory design patterns and having toys as a parent class we can instantiate the iron man toy object or Thor toy object. I hope we have understood what is the objective of the factory design pattern.

 

Formally, a factory design pattern is a creational design pattern. In other words, it is used to create an object. Also, it hides the actual object creation from the developer. The factory design pattern is the best way to handle multiple types of object creation.

 

Uses :

We use a factory design pattern to categorize the object creation. In other words, let’s assume I want to instantiate a database connection object, API object, etc. You know multiple database connections can be used or multiple API authentications can be used in one application. We can create a database connection factory and to handle API authentications we can have API auth factory. Now I want to create an SQL connection object then I can call the connection method and pass the required parameters. Similarly for api auth, if we want to authenticate via oauth, basic or oauth2. These all will be passed as arguments.

Structure :

Parent class: We have a parent class that defines basic functionalities. Let’s call this class Toy.

Children class: We have a children’s class that inherits the parent toy class and overrides default functionalities moreover children’s classes are independent to add more functionalities to it. Like we have parent classes as Toy and children classes as iron man, Thor, etc.

Factory class: We also need a factory class that uses a factory and give the user flexibility to choose to build one of the toy objects. A factory class is generally designed for creating one object by an argument.


Please have a glance at the Javacript code :

				
					// Toy class
class Toy {
    assembling () {}
    export () {}
}

module.exports = Toy
				
			

The toy is the topmost class. That defines the structure of the idol toy class. It gives an idea of what exactly the object will do. what functionalities we are giving to the object.

				
					// Thor - child class
const Toy = require("./Toy");

class Thor extends Toy {
    assembled = false
    exported = false
    assembling () {
        console.log('Constructing Thor toy')
        this.assembled = true
    }
    export () {
        console.log('Exporting Thor toy')
        this.exported = true
    }
}

module.exports = Thor;
				
			
				
					// Ironman - child class
const Toy = require("./Toy");

class Ironman extends Toy {
    assembled = false
    exported = false
    assembling () {
        console.log('Constructing Ironman toy')
        this.assembled = true
    }
    export () {
        console.log('Exporting Ironman toy')
        this.exported = true
    }
}

module.exports = Ironman;
				
			

As shown above, these are the child classes that follow the Toy class’s basic structure. Also, they are capable provider more functionalities than mentioned in the parent class.

				
					// Application
const ToyFactory = require("./ToyFactory");

const ironman = ToyFactory.getToy('ironman')
console.log(ironman);
const thor = ToyFactory.getToy('thor')
console.log(thor);
				
			

Please visit the codebase : 

https://github.com/DeepakAtariya/Design-Patterns-JS/tree/main/Creational-Design-Pattern/FactoryDesignPattern

 

As we have seen factory design patterns can be very useful when it comes to new joiners. The factory design pattern is very common so every programmer will not take much time to understand the code and start working on it. 

 

Like factory design patterns, many design patterns out there solve the daily challenges of a programmer. So if any design pattern is taking time to implement then spend the time because it will save you a lot of time in the future.

 

Thank you,

I will see you with new learnings.