0. Preface
After reading the workshop model for one afternoon, I will summarize it at night, which is also a result.
1. Definition of workshop Mode
The workshop mode can be divided into simple workshop mode and complex workshop mode. In order to dynamically create objects.
2. Category
2.1 simple workshop Model
In the simple workshop mode, you can define a method to create an object based on different parameters (or conditions.
View codeview code
1 var xhr = {2 createxhr: function () {3 VaR methods = [4 function () {return New XMLHttpRequest () ;}, 5 function () {return New activexobject ('Microsoft. XMLHTTP ');} 6]; 7 for (VAR I = 0, Len = methods. length; I <Len; I ++) {// Select 8 try {9 Methods [I] (); 10} catch (e) {11 continue based on the browser; 12} 13 this. createxhr = methods [I]; 14 return methods [I]; 15} 16 throw new error ('can't create xhr'); 17} 18}
2.2 complex workshop Model
This mode defines an interface for creating objects, and then the subclass determines which class to instantiate. Delay the instance of a class to the subclass. It is like a pair of sneakers produced by a gentleman. The specific name is determined by the brand owner.
View code
1 var shoes = { 2 createBrands: function(model){ 3 throw new Error('error'); 4 } 5 } 6 7 var nikeShoes = { 8 createBrands:function(name){ 9 var shoe;10 switch(name){11 case 'Nike 1':shoe = new Nike1();break;12 case 'Nike 2':shoe = new Nike2();break;13 default : shoe = new Nikes();14 }15 return shoe;16 }17 }18 19 var antaShoes = {20 createBrands:function(name){21 var shoe;22 switch(name){23 case 'anta 1':shoe = new Anta1();break;24 case 'atta 2':shoe = new Anta2();break;25 case 'atta 3':shoe = new Anta3();break;26 default : shoe = new Antas();27 }28 return shoe;29 }30 }
3. Personal Summary
1. The workshop mode is divided into simple workshop mode and complex workshop mode.
2. The simple workshop mode determines the class to be instantiated based on different parameters;
3. In the complex workshop mode, the instantiation work is handed over to the subclass to complete. The parent class is just equivalent to an interface.
4. Usage: You must create an object based on the specific environment. It cannot be determined during development and the created object is complex.