mercredi 21 juin 2017

factory pattern in node.js (express)

I was studying design pattern and my favourite is factory pattern. But when I dig more codes written by others I don't see this pattern occur that often, especially on the newer stack.

For instance in this article http://ift.tt/2fMT8Hv the sample look like this:

function SimpleFactory(name) {  
    var factoryName = name;

    var getSalesEmployee = function(firstName, lastName) {
        return {
            firstName: firstName,
            lastName: lastName,
            comission: 0,
            salary: 100,
            projects: [],
            type: 'sales'
        };
    }

    var getEngineerEmployee = function(firstName, lastName) {
        return {
            firstName: firstName,
            lastName: lastName,
            salary: 150,
            manager: '',
            technologies: [],
            projects: [],
            type: 'engineer'
        }
    }

    var getName = function() {
        return factoryName;
    }

    return {
        getSales: getSalesEmployee,
        getEngineer: getEngineerEmployee,
        getName: getName
    }
}

module.exports = {  
    getInstance: SimpleFactory
}

Above code for me is controller, but usually I just write it like this

modue.exports = {

funcA = () => {}
funcB = () => {}

}

and in another file I can just use something.funcA, why do I still need to use factory here? I think commonJs of node just solved the leaking problem.

Aucun commentaire:

Enregistrer un commentaire