Factory #DesignPattern in #JavaScript

Factory is a popular design pattern that we use a lot for creating objects. In Factory pattern, we create object without exposing the…

Factory #DesignPattern in #JavaScript

Factory is a popular design pattern that we use a lot for creating objects. In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.

Using this pattern, to create a new object we use a factory function. A factory function is simply a function that returns an object.

You don’t need to use the new keyword when using factory methods, but it still can be used just like a constructor.

The following is a simple implementation of a factory function which takes in some arguments and uses them to instantiate a new object:

function userFactory(name) {    return {        name: name,        login: function () {            console.log('user ' + name + ' logged in.');        },    };}

And this is how you can use this factory method to create a new user object and call a public method from user object:

var user = userFactory('Aram');  // create a user named Aramuser.login();  // "user Aram logged in."

When you want to make some of your properties or methods private, you can simply just take them out of the returned object. This keeps your implementation details encapsulated, and it will only expose the public interface to your object.

function userFactory(name) {    function completeName() {        return 'Mr or Mrs ' + name;    }    return {        name: name,        login: function () {             console.log('user ' + completeName() + ' logged in.');        },    };}

Cool, Eh?

And similarly, following snippet is how you use this factory method:

var user = userFactory('Aram');  // create a user named Aramuser.login();  // "user Mr or Mrs Aram logged in."user.completeName()  // ERROR: user.completeName is not a function

Also if you are interested to practice Functional Programming in JavaScript, the Factory pattern will be your friend for that purpose.

Found this post useful? Kindly tap the ❤ button below! :)