Factory template vs constructor template in javascript

In javascript, I saw a tutorial on design pattern. Although the textbook was good, he left me a few questions.

As I can see, Factory and constructor give the same result. So what is the difference between the two? What are the usage scenarios for each of them?

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

 function constructorPattern(data) { this.name = data.name; this.title = data.title; this.startDate = data.startDate; } var constructorUse = new constructorPattern(); 

Change As Michael Warner explained. The Factory method returns an immutable object that does not have a reference to its creator after creation.

But in the patter constructor, they have a connection with them.

Thus, for practical use, it would be much better to understand why it is sometimes useful to have an object that has a link to its constructor.

+6
source share
2 answers

Factories create an object and return it. That's all. The created object costs one, and the best thing is that you can use this object, and not be what happens to other objects. This is known as 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. A good part of this sitelink is to use a functional common technique that 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 see after creating the objects, they were still very connected.

To be clear, you can still do the following and not affect the objects created by the constructor. Using the functional general method and mask the prototype function specified by the constructor. Thus, they are not fully connected, but connected through the prototype constructors.

 var car1 = new Car; //running false var car2 = new Car; //running false car2.toggleEngine() //running true car2.toggleEngine = function(){}; car1.toggleEngine() //running true 
+5
source

When using a factory, you have complete freedom for the returned object, so you can choose one constructor or another based on your input or time of day or not create anything at all - when using a factory to return sigletons.

On the contrary, the constructor basically creates an instance of the object with a specific prototype, which you can configure later in the body of the constructor function.

+1
source

All Articles