DEV Community

Hossein Mobarakian
Hossein Mobarakian

Posted on

What is a Factory Design Pattern? It Is For You!

Hi guys, I'm returning after a few years😁, I hope you have a happy day.
In this article I'll explain what Factory is in your project; it may exist in your projects.
Did you think about some libraries that work with some payment methods at the same place with the same code? Or did you think about how cross-platform libraries work?
Yes, You think right! These could have a Factory design pattern under the hood.
So talking is enough, Let's jump into it.

Factory Design Pattern

I try to explain this design pattern with an example for you. Suppose somebody can imitate popular people's voices. For all of the sounds that makes, he will make some jobs(the same job for all of the voices). some jobs like:

  • breading enough
  • changing the larynx situation
  • then start taking

See, these steps are the same but the sound frequency is different.
You can think about these steps as an interface and their implementation, and different voices will use this interface for their implementation according to these steps.
if some imitator can imitate 4 voices, he has 4 classes with that interface, and when he wants he can switch between voices without changing executing steps.
So this is the reason why imitator can imitate voices because they know and they have an interface in their body😁.
When we use factory design in our project or our body, we can learn and implement related things and knowledge faster and easier, without the need for huge changes. That is why some can learn better about languages than others or exercise better than others.
You should have this approach in your projects if your project can have expansion in some part in the future.

You can see code samples and more technical information about Factory design patterns in this beautiful website:
https://refactoring.guru/design-patterns/factory-method

Let's have some fun:🎶

Top comments (0)