JavaScript设计模式-9.工厂模式

Posted 橙云生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript设计模式-9.工厂模式相关的知识,希望对你有一定的参考价值。

  1 <!DOCTYPE html>
  2 <html>
  3     <head>
  4         <meta charset="UTF-8">
  5         <title>javascript高级语法9-工厂模式</title>
  6     </head>
  7     <body>
  8         <script>
  9             /*1.简单工厂 :通过第三方的类完成松耦合的任务 ->工厂。
 10              * 2.复杂工厂:通过把实例化的任务交个子类来完成,用来达到松耦合的目的 ->工厂。
 11              * 3.超级工厂:通过eval来完成智能工厂。
 12              */
 13             /*工厂的目的在于判别接口最终用哪类来实例化、
 14              * 产生实例的过程不用new关键字
 15              * 最终达到的效果是多台,类与类之间的松耦合
 16              */
 17             
 18             //需要用到的继承和接口方法    
 19             function extend(subClass,superClass){
 20                 //1.叫子类原型类属性等于父类的原型属性
 21                 //初始化一个中间空对象,为了转换主父类关系
 22                 var F = function(){};
 23                 F.prototype = superClass.prototype;
 24                 //2.让子类集成F
 25                 subClass.prototype = new F();
 26                 subClass.prototype.constructor = subClass;
 27                 //3.为子类增加属性superClass
 28                 subClass.superClass = superClass.prototype;
 29                 //4.增加一个保险,就算你是的原型类是超类(Object) 那么也要把你的构造函数级别讲下来
 30                 if(superClass.prototype.constructor == Object.prototype.constructor){
 31                     superClass.prototype.constructor = superClass;
 32                 }
 33             }
 34             //接口
 35             var Interface = function(name,methods){
 36                 if(arguments.length != 2){
 37                     alert("interface must have two paramters...");
 38                 }
 39                 this.name = name;//这个是接口的名字
 40                 this.methods = [];//定义个空数组来转载函数名
 41                 for (var i = 0; i < methods.length; i++) {
 42                     if(typeof methods[i] != "string"){
 43                         alert("method name must is String ...")
 44                     }else{
 45                         this.methods.push(methods[i])
 46                     }
 47                 }
 48             }
 49             //定义接口的一个静态方法来实现接口与实现类的直接检验
 50             //静态方法不要写成Interface.prototype.* 因为这是写到接口原型连上的
 51             //我们要把静态的函数直接写到类层次上
 52             Interface.ensureImplements = function(object){
 53                 if(arguments.length<2){
 54                     alert("必须最少是2个参数");
 55                     return false;
 56                 }
 57                 //遍历
 58                 for (var i = 1; i < arguments.length; i++) {
 59                     var inter = arguments[i];
 60                     //如果你是接口就必须是Interface类型的
 61                     if(inter.constructor != Interface){
 62                         throw new Error("if is interface class must is Interface type");
 63                     }
 64                     //遍历函数集合并分析
 65                     for (var j = 0; j < inter.methods.length; j++) {
 66                         var method = inter.methods[j];
 67                         //实现类中必须有方法名字 和 接口中所有的方法名项目
 68                         if(!object[method] || typeof object[method] != "function"){
 69                             throw new Error("实现类并且没有完全实现接口中的所有方法...");
 70                         }
 71                     }
 72                 }
 73             }
 74 
 75             function factory1(){
 76                 var Pet = new Interface("Pet",["eat","run","sing","register"]);
 77                 //宠物店
 78                 var PetShop = function(){}
 79                 PetShop.prototype = {
 80                     //出售宠物的方法
 81                     sellPetShop:function(kind){
 82                         //宠物对象
 83                         var pet;
 84                         //kind种类
 85                         switch(kind){
 86                             case dog:
 87                                 pet = new Dog();
 88                                 break;
 89                             case cat:
 90                                 pet = new Cat();
 91                                 break;
 92                             case pig:
 93                                 pet = new Pig();
 94                                 break;
 95                             default:
 96                                 //
 97                                 pet = new Bird();
 98                         }
 99                         //验证接口
100                         Interface.ensureImplements(pet,Pet);
101                         pet.eat();
102                         pet.register();
103                         return pet;
104                     }
105                 }
106                 //宠物基类
107                 function BasePet(){
108                     this.register = function(){
109                         document.write("宠物登记 。。。<br>")
110                     }
111                     this.eat = function(){
112                         document.write("吃顿饱饭 <br>")
113                     }
114                 }
115                 //实现
116                 function Dog(){
117                     Dog.superClass.constructor.call(this);
118                     this.run = function(){
119                         document.write("小狗跑步 。。<br>")
120                     }
121                     this.sing = function(){
122                         document.write("小狗唱歌。。。<br>")
123                     }
124                 }
125                 function Cat(){
126                     Cat.superClass.constructor.call(this);
127                     this.run = function(){
128                         document.write("小猫跑步 。。<br>")
129                     }
130                     this.sing = function(){
131                         document.write("小猫唱歌。。。<br>")
132                     }
133                 }
134                 function Pig(){
135                     Pig.superClass.constructor.call(this);
136                     this.run = function(){
137                         document.write("小猪跑步 。。<br>")
138                     }
139                     this.sing = function(){
140                         document.write("小猪唱歌。。。<br>")
141                     }
142                 }
143                 function Bird(){
144                     Bird.superClass.constructor.call(this);
145                     this.run = function(){
146                         document.write("小鸟跑步 。。<br>")
147                     }
148                     this.sing = function(){
149                         document.write("小鸟唱歌。。。<br>")
150                     }
151                 }
152                 
153                 //继承
154                 extend(Dog,BasePet);
155                 extend(Pig,BasePet);
156                 extend(Bird,BasePet);
157                 extend(Cat,BasePet);
158                 
159                 //pcat宠物店
160                 var pcatPetShop = new PetShop();
161                 var flowerPig = pcatPetShop.sellPetShop("pig");
162                 flowerPig.run();
163                 
164                 /*貌似很完美,但是他禁不住需求的变化
165                  * 比如说宠物商店又进来一些新的品种宠物
166                  * 这时候用目前的方法必须要修改宠物商店这个类
167                  * 用一个简单工厂来解决
168                  */
169                 
170             }
171 //            factory1();
172             
173             
174             function factory2(){
175                 //静态工厂
176                 var Pet = new Interface("Pet",["eat","run","sing","register"]);
177                 var PetFactory = {
178                     sellPetShop:function(kind){
179                         //宠物对象
180                         var pet;
181                         //kind种类
182                         switch(kind){
183                             case dog:
184                                 pet = new Dog();
185                                 break;
186                             case cat:
187                                 pet = new Cat();
188                                 break;
189                             case pig:
190                                 pet = new Pig();
191                                 break;
192                             default:
193                                 //
194                                 pet = new Bird();
195                         }
196                         //验证接口
197                         Interface.ensureImplements(pet,Pet);
198                         return pet;
199                     }
200                 }
201                 //利用工厂的新宠物店
202                 var PetShop2 = function(){}
203                 PetShop2.prototype = {
204                     sellPetShop:function(kind){
205                         var pet = PetFactory.sellPetShop(kind);
206                         pet.eat();
207                         pet.register();
208                         return pet;
209                     }
210                 }
211                 
212                 //宠物基类
213                 function BasePet(){
214                     this.register = function(){
215                         document.write("宠物登记 。。。<br>")
216                     }
217                     this.eat = function(){
218                         document.write("吃顿饱饭 <br>")
219                     }
220                 }
221                 //实现
222                 function Dog(){
223                     Dog.superClass.constructor.call(this);
224                     this.run = function(){
225                         document.write("小狗跑步 。。<br>")
226                     }
227                     this.sing = function(){
228                         document.write("小狗唱歌。。。<br>")
229                     }
230                 }
231                 function Cat(){
232                     Cat.superClass.constructor.call(this);
233                     this.run = function(){
234                         document.write("小猫跑步 。。<br>")
235                     }
236                     this.sing = function(){
237                         document.write("小猫唱歌。。。<br>")
238                     }
239                 }
240                 function Pig(){
241                     Pig.superClass.constructor.call(this);
242                     this.run = function(){
243                         document.write("小猪跑步 。。<br>")
244                     }
245                     this.sing = function(){
246                         document.write("小猪唱歌。。。<br>")
247                     }
248                 }
249                 function Bird(){
250                     Bird.superClass.constructor.call(this);
251                     this.run = function(){
252                         document.write("小鸟跑步 。。<br>")
253                     }
254                     this.sing = function(){
255                         document.write("小鸟唱歌。。。<br>")
256                     }
257                 }
258                 
259                 //继承
260                 extend(Dog,BasePet);
261                 extend(Pig,BasePet);
262                 extend(Bird,BasePet);
263                 extend(Cat,BasePet);
264                 
265                 var pcatPetShop2 = new PetShop2();
266                 var flowerCat = pcatPetShop2.sellPetShop("cat");
267                 flowerCat.sing();
268                 
269                 /*貌似很完美
270                  * 新的需求:都是宠物店
271                  * 张三的店主要卖哈士奇,李四的店卖各种鸟
272                  */
273             }
274             
275 //            factory2();
276             
277             function factory3(){
278                 var Pet = new Interface("Pet",["eat","run","sing","register"]);
279                 //宠物店
280                 var PetShop = function(){}
281                 PetShop.prototype = {
282                     //出售宠物的方法
283                     sellPetShop:function(kind){
284                         //宠物对象
285                         var pet;
286                         //kind种类
287                         switch(kind){
288                             case dog:
289                                 pet = new Dog();
290                                 break;
291                             case cat:
292                                 pet = new Cat();
293                                 break;
294                             case pig:
295                                 pet = new Pig();
296                                 break;
297                             default:
298                                 //
299                                 pet = new Bird();
300                         }
301                         //验证接口
302                         Interface.ensureImplements(pet,Pet);
303                         pet.eat();
304                         pet.register();
305                         return pet;
306                     }
307                 }
308                 //宠物基类
309                 function BasePet(){
310                     this.register = function(){
311                         document.write("宠物登记 。。。<br>")
312                     }
313                     this.eat = function(){
314                         document.write("吃顿饱饭 <br>")
315                     }
316                 }
317                 //实现
318                 function Dog(){
319                     Dog.superClass.constructor.call(this);
320                     this.run = function(){
321                         document.write("小狗跑步 。。<br>")
322                     }
323                     this.sing = function(){
324                         document.write("小狗唱歌。。。<br>")
325                     }
326                 }
327                 function Cat(){
328                     Cat.superClass.constructor.call(this);
329                     this.run = function(){
330                         document.write("小猫跑步 。。<br>")
331                     }
332                     this.sing = function(){
333                         document.write("小猫唱歌。。。<br>")
334                     }
335                 }
336                 function Pig(){
337                     Pig.superClass.constructor.call(this);
338                     this.run = function(){
339                         document.write("小猪跑步 。。<br>")
340                     }
341                     this.sing = function(){
342                         document.write("小猪唱歌。。。<br>")
343                     }
344                 }
345                 function Bird(){
346                     Bird.superClass.constructor.call(this);
347                     this.run = function(){
348                         document.write("小鸟跑步 。。<br>")
349                     }
350                     this.sing = function(){
351                         document.write("小鸟唱歌。。。<br>")
352                     }
353                 }
354                 
355                 //继承
356                 extend(Dog,BasePet);
357                 extend(Pig,BasePet);
358                 extend(Bird,BasePet);
359                 extend(Cat,BasePet);
360                 
361                 //1.把核心的商店变成一个抽象类
362                 var Petshop = function(){}
363                 Petshop.prototype = {
364                     sellPetShop:function(kind){
365                         var pet = this.sellPetshop(kind);
366                         pet.eat();
367                         pet.register();
368                         return pet;
369                     },
370                     sellPetshop:function(model){
371                         throw new Error("this is abstract class")
372                     }
373                 }
374                 //2.利用子类满足上边的需求(多态)
375                 var OnePetShop = function(){}
376                 extend(OnePetShop,Petshop);
377                 OnePetShop.prototype.sellPetshop = function(model){
378                     var pet;
379                         //kind种类
380                         switch(model){
381                             case dog:
382                                 pet = new Dog();
383                                 break;
384                             default:
385                                 //
386                                 pet = new Bird();
387                         }
388                         //验证接口
389                         Interface.ensureImplements(pet,Pet);
390                         pet.eat();
391                         pet.register();
392                         return pet;
393                 }
394                 
395                 var TwoPetShop = function(){}
396                 extend(TwoPetShop,Petshop);
397                 TwoPetShop.prototype.sellPetshop = function(model){
398                     var pet;
399                         //kind种类
400                         switch(model){
401                             case pig:
402                                 pet = new Pig();
403                                 break;
404                             default:
405                                 //
406                                 pet = new Bird();
407                         }
408                         //验证接口
409                         Interface.ensureImplements(pet,Pet);
410                         pet.eat();
411                         pet.register();
412                         return pet;
413                 }
414                 
415                 //实验
416                 var jim = new OnePetShop();
417                 var dog = jim.sellPetshop("dog");
418                 dog.run();
419                 
420                 var tom = new TwoPetShop();
421                 var pig = tom.sellPetshop("pig");
422                 pig.run();
423             }
424             
425 //            factory3();
426             
427             function factory4(){
428                 var Pet = new Interface("Pet",["eat","run","sing","register"]);
429                 //宠物基类
430                 function BasePet(){
431                     this.register = function(){
432                         document.write("宠物登记 。。。<br>")
433                     }
434                     this.eat = function(){
435                         document.write("吃顿饱饭 <br>")
436                     }
437                 }
438                 //实现
439                 function Dog(){
440                     Dog.superClass.constructor.call(this);
441                     this.run = function(){
442                         document.write("小狗跑步 。。<br>")
443                     }
444                     this.sing = function(){
445                         document.write("小狗唱歌。。。<br>")
446                     }
447                 }
448                 function Cat(){
449                     Cat.superClass.constructor.call(this);
450                     this.run = function(){
451                         document.write("小猫跑步 。。<br>")
452                     }
453                     this.sing = function(){
454                         document.write("小猫唱歌。。。<br>")
455                     }
456                 }
457                 function Pig(){
458                     Pig.superClass.constructor.call(this);
459                     this.run = function(){
460                         document.write("小猪跑步 。。<br>")
461                     }
462                     this.sing = function(){
463                         document.write("小猪唱歌。。。<br>")
464                     }
465                 }
466                 function Bird(){
467                     Bird.superClass.constructor.call(this);
468                     this.run = function(){
469                         document.write("小鸟跑步 。。<br>")
470                     }
471                     this.sing = function(){
472                         document.write("小鸟唱歌。。。<br>")
473                     }
474                 }
475                 
476                 //继承
477                 extend(Dog,BasePet);
478                 extend(Pig,BasePet);
479                 extend(Bird,BasePet);
480                 extend(Cat,BasePet);
481                 
482                 //智能工厂
483                 var PetFactory = {
484                     sellPetShop:function(kind){
485                         var pet;
486                         pet= eval("new "+kind +"()");
487                         Interface.ensureImplements(pet,Pet);//在工厂中验证接口关系。
488                         return pet;
489                     }
490                 }
491                 
492                 //1.把核心的商店变成一个抽象类
493                 var Petshop = function(){}
494                 Petshop.prototype = {
495                     sellPetShop:function(kind){
496                         var pet = this.sellPetshop(kind);
497                         pet.eat();
498                         pet.register();
499                         return pet;
500                     },
501                     sellPetshop:function(model){
502                         throw new Error("this is abstract class")
503                     }
504                 }
505                 //2.利用子类满足上边的需求(多态)
506                 var OnePetShop = function(){}
507                 extend(OnePetShop,Petshop);
508                 OnePetShop.prototype.sellPetshop = function(model){
509                         var pet=null;
510                         var pets = ["Dog","Cat","Bird"];
511                         for( v in pets){
512                             if(pets[v] == model){
513                                 pet = PetFactory.sellPetShop(model);
514         //                        Interface.ensureImplements(pet,Pet);
515                                 pet.eat();
516                                 pet.register();
517                                 break;
518                             }
519                         }
520                         return pet;
521                 }
522                 
523                 var TwoPetShop = function(){}
524                 extend(TwoPetShop,Petshop);
525                 TwoPetShop.prototype.sellPetshop = function(model){
526                     var pet=null;
527                         var pets = ["Pig"];
528                         for( v in pets){
529                             if(pets[v] == model){
530                                 pet = PetFactory.sellPetShop(model);
531         //                        Interface.ensureImplements(pet,Pet);
532                                 pet.eat();
533                                 pet.register();
534                                 break;
535                             }
536                         }
537                         return pet;
538                 }
539                 
540                 //测试
541                 var twoPetShop = new TwoPetShop();
542                 twoPetShop.sellPetshop("Pig");
543                 
544                 var jim = new OnePetShop();
545                     jim.sellPetshop("Dog");
546 //                    jim.sellPetShop("Pig");
547             }
548             factory4();
549         </script>
550     </body>
551 </html>

 

以上是关于JavaScript设计模式-9.工厂模式的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 设计模式之工厂模式

《JavaScript设计模式 张》整理

Javascript设计模式第二课 神奇的魔术师——简单工厂模式

PHP面向对象之选择工厂和更新工厂

Javascript设计模式-工厂模式

JavaScript创建对象之单例工厂构造函数模式