I saw a tutorial about design pattern in javascript. Although tutorial was good it left me with few question.
As I see Factory and constructor produce the same result. So what is the difference between both of them? What are the use case scenarios for each one?
Factory pattern
function factoryPattern(data) {
var factory = {};
factory.name = data.name;
factory.title = data.title;
factory.startDate = data.startDate;
return factory;
}
var factoryUse = factoryPattern(jsonObj);
Constructor pattern
function constructorPattern(data) {
this.name = data.name;
this.title = data.title;
this.startDate = data.startDate;
}
var constructorUse = new constructorPattern();
Edit: As explained by @Michael Warner. Factory method return immutable object which has no link to their creater after they created.
But in constructor patter they do have link with them.
So having a practical use case would be much better to understand why sometimes is good to have object which has link to their constructor.
The Factory Function is similar to constructor functions/class functions, but instead of using new to create an object, factory functions simply creates an object and returns it. Factory Functions are a very useful tool in JavaScript.
The factory pattern is a creational design pattern that provides a generic interface for creating objects. In the factory pattern, we can specify the type of object being created and we do not need to explicitly require a constructor.
Factories are much more flexible than either constructor functions or classes, and they don't lead people down the wrong path by tempting them with the `extends` keyword and deep inheritance hierarchies. There are many safer code reuse mechanisms you should favor over class inheritance, including functions and modules.
Factory functions are mainly used when the user wants to initialize the object of a class multiple times with some assigned value or static values. It makes the process easy since we just have to call this function and retrieve the new object created.
Factories create an object and returns it. That is about it. The object that is created stands alone and the best part about this is you can use that object and not be effected by what happens to the other objects. This is know as a singleton.
var Car = function(){
var car = {};
car.running = false;
car.toggleEngine = function(){
this.running = !this.running;
}
return car;
};
car1 = Car(); // running false
car2 = Car(); // running false
car1.toggleEngine(); // running true
car2.toggleEngine = undefined; // to broke down.
car1.toggleEngine(); //running false
Constructors add code to the function so you have a link to the prototype of the object constructor. The nice part about this additional link is to use the functional shared technique which looks like this.
var Car = function (){
this.running = false;
};
Car.prototype.toggleEngine = function(){
this.running = !this.running;
}
var car1 = new Car; //running false
var car2 = new Car; //running false
car2.toggleEngine() //running true
Car.prototype.toggleEngine = function(){};
car1.toggleEngine() //running false
As we can see after the objects were created they still were very much linked together.
To be clear you can still do the following and not effect the objects created by the constructor. With functional shared method and mask the prototype function given by the constructor. So there are not fully linked but they are linked through the constructors prototype.
var car1 = new Car; //running false
var car2 = new Car; //running false
car2.toggleEngine() //running true
car2.toggleEngine = function(){};
car1.toggleEngine() //running true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With