We are beginning to learn Node.js and we are not always sure if we do it the right way. We have a strong object oriented background so the adoption is not always easy. Right now we implemented a factory method, but we are not completely sure if our approach is good.
Factory Method:
var fs = require('fs');
module.exports.getHandler = function(type,callback) {
if(!type) {
throw new Error('Please specifiy a type');
}
var nodeModule = type.ucFirst()+'FieldHandler';
var file = __dirname+'/'+nodeModule+'.js';
fs.exists(file, function(exists) {
if (exists) {
var handler = require('./'+nodeModule);
var instance = new handler;
callback(instance);
} else {
throw new Error('Handler for "'+type+'" does not exist. ' +
'Expect file of handler to have the name "'+file+'"');
}
});
};
Object which uses the factory method:
var FieldhandlerFactory = require('./fieldhandler/Factory');
...
...
...
var handleField = function (actualField) {
var callbackHandler = function (handler) {
handler.setJson(actualField);
handler.check();
fieldString += handler.toView() + '\n';
actual++;
if (amountFields == actual) {
// WE ARE READY DO SOMETHING
}
};
var handler = FieldhandlerFactory.getHandler(type, callbackHandler);
}
for (var index in fields) {
var field = fields[index];
type = field.type;
if (!type) {
throw new Error('Field on index "' + index + '" has no attribute type');
}
var string = handleField(field);
}
...
...
...
The code works, so we are quite happy. Is this a good approach or do we stick too much to the classical OOP style? Especially with testing we face some problems. How to test the function handleField isolated? And with jasmine-node I'm not able to catch the exception in the factory method when the file is not found.
So we think there are tons of improvements. We are looking forward to learn from you and to exchange experiences.