Design Pattern (Factories) + NodeJs

Renan Deocleciano
2 min readApr 16, 2021

This article is part of my channel on Youtube. So, if you like share with your friends.

Factories — Generic creation of interfaces

In this article I’m going to talk about the creation design pattern called Factory.

This pattern is exactly what its name says, The Factory! So, If we need to think about the concept, this pattern is used to create new user interfaces and it’s very simple.

Are we to the real world to make this concept easier to understand?

Look at this scenario:
A user requests some reports and with that request he needs to pass a filter and that filter has a report type.

So, let’s go back to the code and create this scenario with the Factory Pattern.

Looking at the image, it’s possible to identify our structure:

1 — App.js
2 — ReportFactory.js
3 — Report.js
4 — Company1.js
5 — Company2.js
6 — Company3.js

First, we need to create the Report file with the sole responsibility: Show company information.

Report.js

Second, we need to create your models that Factory is responsible for building. Then, create the following files: Company[1..3].js and let’s go to work!

Company1.js

Company2.js

Company3.js

After that, we really need to create our Factory. This class has the responsibility to create a report with a specific type. To do this, follow the code bellow:

ReportFactory.js

Our structure is ready. Now, create the app file and put the code below:

App.js

Obviously, this is a simple example. but imagine a web application and this type received on demand.
If you liked it, please click on the link of my channel on YouTube and click on the like button to support me.

--

--