Deep understanding of the JavaScript series (28): factory mode of design patterns, deep understanding of javascript
Introduction
Similar to the creation mode, when creating an object in the factory mode (as a product in the factory), you do not need to specify a specific class for creating the object.
The factory mode defines an interface for creating objects. The subclass determines which class to instantiate. This mode delays the instantiation of a class to the subclass. Subclass can override the interface method to specify its own object type during creation.
This mode is very useful, especially when the process of creating an object is assigned a value, for example, depending on many configuration files. In addition, you will often see the factory method in the program to let the subclass class define the object type to be created.
Body
In the following example, the improved version of the constructor pattern code in Chapter 26th is applied to the factory method:
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:
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 still don't understand it, let's look at it in detail. If we want to insert some elements into the webpage, and these element types are not fixed, they may be images, it may also be a connection or even a text. According to the definition of the factory mode, we need to define the factory class and the corresponding subclass. Let's first define the concrete implementation of the subclass (that is, the subfunction):
Var page = page | {}; page. dom = page. dom | {}; // subfunction 1: process text pages. dom. text = function () {this. insert = function (where) {var txt = document. createTextNode (this. url); where. appendChild (txt) ;};}; // subfunction 2: process the 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: process the image 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 is actually very simple:
page.dom.factory = function (type) { return new page.dom[type];}
The usage is as follows:
var o = page.dom.factory('Link');o.url = 'http://www.cnblogs.com';o.insert(document.body);
So far, the introduction of the factory model is clear to everyone, so I will not describe it more.
Summary
When to use the factory Model
Factory models are particularly useful in the following scenarios:
- Object Construction is very complex
- Different instances need to be created depending on the specific environment
- Process a large number of small objects with the same attributes
When should I use the factory model?
Without misuse of the factory model, sometimes it only adds unnecessary complexity to the code and makes the test difficult to run.
Copyright statement: This article is the original author of the http://www.zuiniusn.com, not allowed by the blogger can not be reproduced.