I came across a case while refactoring a piece of code that required me to use object factory pattern in nodejs. So I tried my hands with this pattern in sample code and came up with some doubts.
factoryTest.js
var createFactory = require("./createFactory").createObjects();
var helloWorldObject = createFactory.helloWorld("userOne");
console.log("helloWorldObject: "+JSON.stringify(helloWorldObject));
var byeWorldObject = createFactory.byeWorld("userTwo", 22);
console.log("helloWorldObject: "+JSON.stringify(helloWorldObject));
console.log("byeWorldObject: "+JSON.stringify(byeWorldObject));
createFactory.js
var createObjects = function() {
console.log("received a request to create new object.")
this.localObject = {};
};
createObjects.prototype.helloWorld = function(name){
this.localObject = {}; // CASE 1 <<<--- how can this be removed?
this.localObject.name = name;
this.localObject.greeting = "Hello " +name;
return this.localObject;
};
createObjects.prototype.byeWorld = function(name, age){
this.localObject = {}; // CASE 2 <<<--- how can this be removed?
this.localObject.name = name;
this.localObject.age= age;
this.localObject.greeting = "Bye " +name;
return this.localObject;
};
exports.createObjects = function(){
return new createObjects();
}
- Is the way of calling and creating new objects as per object factory design pattern?
- Can CASE 1 and CASE 2 creation as empty object be removed from helloWorld and byeWorld(marked as a comment in code above)?
- Does factory pattern create a single object that gets used with each request OR Does factory pattern create new objects for each request as "new"?
Output when CASE1 and CASE2 are kept as is
received a request to create new object.
helloWorldObject: {"name":"userOne","greeting":"Hello userOne"}
helloWorldObject: {"name":"userOne","greeting":"Hello userOne"}
byeWorldObject: {"name":"userTwo","age":22,"greeting":"Bye userTwo"}
Output when CASE1 and CASE2 are removed
received a request to create new object.
helloWorldObject: {"name":"userOne","greeting":"Hello userOne"}
helloWorldObject: {"name":"userTwo","greeting":"Bye userTwo","age":22}
byeWorldObject: {"name":"userTwo","greeting":"Bye userTwo","age":22}
New version:
createFactory.js
var objectFactory = function(){
console.log("received a new factory request.")
return new createObjects();
}
var createObjects = function() {
console.log("received a request to create new object.");
this.localObject = {};
};
createObjects.prototype.helloWorld = function(name){
this.localObject.name = name;
this.localObject.greeting = "Hello " +name;
return this.localObject;
};
createObjects.prototype.byeWorld = function(name, age){
this.localObject.name = name;
this.localObject.age = age;
this.localObject.greeting = "Bye " +name;
return this.localObject;
};
module.exports.objectFactory = objectFactory;
factoryTest.js
var createFactory = require("./createFactory").objectFactory;
var helloWorldObject = createFactory().helloWorld("userOne");
console.log("helloWorldObject: "+JSON.stringify(helloWorldObject));
var byeWorldObject = createFactory().byeWorld("userTwo", 22);
console.log("helloWorldObject: "+JSON.stringify(helloWorldObject));
console.log("byeWorldObject: "+JSON.stringify(byeWorldObject));
Output
received a new factory request.
received a request to create new object.
helloWorldObject: {"name":"userOne","greeting":"Hello userOne"}
received a new factory request.
received a request to create new object.
helloWorldObject: {"name":"userOne","greeting":"Hello userOne"}
byeWorldObject: {"name":"userTwo","age":22,"greeting":"Bye userTwo"}
Does this approach eliminate issues?