introduce
Similar to the creation mode, when creating an object (considered as a product in the factory), there is no need to specify the specific class to create the object.
The factory pattern defines an interface for creating objects, and this interface is determined by the subclass to instantiate which class. This pattern delays the instantiation of a class to a subclass. Subclasses can override interface methods so as to specify their own object type when creating them.
This mode is very useful, especially when creating process assignments for objects, such as relying on many settings files. Also, you will often see factory methods in your program to make the subclass class define the object type that needs to be created.
text
In the following example, an improved version of the constructor pattern code in Chapter 26 using the factory method:
The code copy is as follows:
var Car = (function () {
var Car = function (model, year, miles) {
this.model = model;
this.year = year;
this.miles = miles;
};
return function (model, year, miles) {
return new Car(model, year, miles);
};
})();
var tom = new Car("Tom", 2009, 20000);
var dudu = new Car("Dudu", 2010, 5000);
If it is hard to understand, let's give another example:
The code copy is as follows:
var productManager = {};
productManager.createProductA = function () {
console.log('ProductA');
}
productManager.createProductB = function () {
console.log('ProductB');
}
productManager.factory = function (typeType) {
return new productManager[typeType];
}
productManager.factory("createProductA");
If we don't understand it yet, then let's be more detailed. If we want to insert some elements into the web page, and these elements are not fixed in types, they may be pictures, connections, or even text. According to the definition of the factory pattern, we need to define the factory class and the corresponding subclass. Let's first define the specific implementation of the subclass (that is, the subfunction):
The code copy is as follows:
var page = page || {};
page.dom = page.dom || {};
//Subfunction 1: Processing text
page.dom.Text = function () {
this.insert = function (where) {
var txt = document.createTextNode(this.url);
where.appendChild(txt);
};
};
//Subfunction 2: Processing link
page.dom.Link = function () {
this.insert = function (where) {
var link = document.createElement('a');
link.href = this.url;
link.appendChild(document.createTextNode(this.url));
where.appendChild(link);
};
};
//Subfunction 3: Processing pictures
page.dom.Image = function () {
this.insert = function (where) {
var im = document.createElement('img');
im.src = this.url;
where.appendChild(im);
};
};
So how do we define factory processing functions? It's actually very simple:
The code copy is as follows:
page.dom.factory = function (type) {
return new page.dom[type];
}
How to use it is as follows:
The code copy is as follows:
var o = page.dom.factory('Link');
o.url = 'http://www.cnblogs.com';
o.insert(document.body);
At this point, I believe everyone has already understood the introduction of the factory model, so I will not describe it any more.
Summarize
When to use factory mode
The factory model is particularly useful in the following scenarios:
1. The construction of objects is very complex
2. You need to rely on the specific environment to create different instances
3. Handle a large number of small objects with the same attributes
When shouldn't use factory mode
Do not abuse the factory model, sometimes it just adds unnecessary complexity to the code, and makes it difficult to run the test.