Factory Design Pattern, is a part of creational design pattern, where we define the interface to creating the object but letโs subclass decide which class to instantiate.
Factroy design pattern create object without exploring the creation logic to the client and refer to the newly created object using a common interface.
Bit confused :(, right?
Let’s understand this with using simple real life example:
lets suppose you have a smartphones manufacture company, where you manufacture a smartphone, When you launch your first smartphone to the market, people really love it and by looking into the popularity of your smartphone, different vendor comes with the different requirements/features, which they want to include in their smartphones.
What’s problem here?
Now, the problem is, your code is tightly coupled around with your smartphone, if you want to manufacture different smartphones with different feature, you have to make lots of code changes in your codebase.
And what happen if some other vendor comes up with different feature (like they want 16GB RAM, 232GB Internal memory etc, 34px camera etc.), again you have to make changes in your code base on the different requirements.
Now, after sometimes your code become messi and difficult to manage.
Here is the solution ๐
Factory design pattern helps you to overcome the above problem.
Factory design pattern allows you to replace the direct obejct construction call with calls to factory method. Object returns by a factory method are often reffered to as Products.

Thers is a slight limitations, subclass may retrurn different types of Phones only if these phones have a common base class or interface. Also, the factory method in the base class should have its return type declared as this interface.

For example, createSamsungProduct() and createOnePlusProduct() classes should implement the IProduct interface, which declares a method called createProduct(). Each class implements this method differently: createSamsungProduct() create product with different make, model and other features and createOnePlusProduct() create product with different make, model and other features.
Class Diagram / Structure

Here, client uses the factory to create the different types of smartphones.
Now if any vendor comes up with different requirement, you only need to create a new ProductFactory subclass and override the createProduct() method in it.
When to use factory design pattern:
- The objects need to be extended to subclasses.
- The classes does not know about what exact subclasses has to create.
- The product implementation tend to change over the time and the client remains same.
- Use the Factory Method when you want to save system resources by reusing existing objects instead of rebuilding them each time.
Pros and Cons of Factory Design Pattern
Pros:
- Single Responsibilty Principle : you move the product creation code into one place in the program, making the code easier to support.
- Open Closed Principle: You can introduce a new type of prodcuts into the program without breaking the existing client code.
Cons:
- The code become more complex as you have to introduce lots of sub classes to implement this pattern.
Psuedo Code (Language : TypeScript)
client.ts

productFactory.ts (factort design pattern implementation)

createProduct.ts (Implents iterface IProduct)

IProduct.ts (Interface)

Output:

GitHub Link: Factory Design Pattern Implementation
Thank You ! ๐
Interesting explanation!! Great job!
LikeLike
Thank you ๐
LikeLike
Thanks Akshay.
Sorry for the delay, I was bit busy.
LikeLike
Thank you ๐
LikeLike