Skip to main content

What is Factory Design Pattern and how can we implement it in Nodejs

First lets understand what Design Pattern means .In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.

Intent of Factory Pattern - Define an interface for creating an object but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses

Now lets jump directly to code by creating basic Person class...

Next we will create Shopper class extending Person class...

Now lets create another class named Employee...

After creating classes we will define function named userFactory which will be used to instantiate classes Employee and Shopper..

Now lets use userFactory function to create instances of Employee and Shopper classes...

Hope you like the post. See you again on next post...


Comments

Popular posts from this blog

What is Command Design Pattern and how to implement it?

Intent - Encapsulate a request as an object thereby letting you parameterize with different requests , queue or log requests and support undoable options Now we will create two classes exit command will exit out of nodejs and create command will create new file. Now the interesting part of this post comes where we will define a class with help of which we will be able to run above defined commands , undo previously run command or redo previously undo command. At last with help of readline module we will take input from user of command which is to be executed.. Hope you like the post.Keep learning...

Making axios request using redux thunk react

How to integrate redux thunk with react? First run below command to get redux thunk installed Now add the redux thunk configuration to index.js file of your project Below is the sample folder structure to be followed.. Firstly lets code actions related to creating video on route /video/create Now at last lets make reducer function to handle actions dispatched Hope you like the post...