- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
 
- "http://www.w3.org/TR/html4/loose.dtd">  
 
- <html xmlns="http://www.w3.org/1999/xhtml">  
 
-     <head>  
 
-         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
 
-         <script charset="UTF-8" type="text/javascript">  
 
-             /**  
 
-              * 创建接口类  
 
-              */  
 
-               
 
-             function Interface(name,methods){  
 
-                 if(arguments.length<2){  
 
-                     throw new Error(‘需要传递两个参数‘);  
 
-                 }  
 
-                 this.name = name;  
 
-                 this.methods = [];  
 
-                 for(var i = 1;i<methods.length;i++){  
 
-                     var methodName = methods[i];  
 
-                     if(typeof methodName !==‘string‘){  
 
-                         throw new Error(‘方法要为字符串类型‘);  
 
-                     }  
 
-                     this.methods.push(methodName);  
 
-                 }  
 
-             }  
 
-               
 
-             var CompositeInterface = new Interface(‘CompositeInterface‘,[‘add‘,‘remove‘]);  
 
-             var FormItemInterface = new Interface(‘FormItemInterface‘,[‘select‘,‘update‘]);  
 
-             /**  
 
-              * 创建实现类  
 
-              */  
 
-             function MyImpl(){  
 
-                   
 
-             }  
 
-               
 
-             /**  
 
-              * 实现接口  
 
-              */  
 
-             MyImpl.prototype.add = function(){  
 
-                 alert(‘add...‘);  
 
-             }  
 
-               
 
-             MyImpl.prototype.remove = function(){  
 
-                 alert(‘remove...‘);  
 
-             }  
 
-             MyImpl.prototype.select = function(){  
 
-                 alert(‘select...‘);  
 
-             }  
 
-             // MyImpl.prototype.update = function(){  
 
-                 // alert(‘update...‘);  
 
-             // }  
 
-               
 
-             /**  
 
-              * 检测是否实现接口中的所有方法  
 
-              */  
 
-             Interface.ensureImplements = function(object){  
 
-                 if(arguments.length<2){  
 
-                     throw Error(‘参数个数不能小于2个‘);  
 
-                 }  
 
-                 for(var j=1;j<arguments.length;j++){  
 
-                     var interfaceInstance = arguments[j];  
 
-                     if(interfaceInstance.constructor !==Interface){  
 
-                         throw new Error(interfaceInstance+‘不是所需接口实例‘);  
 
-                     }  
 
-                   
 
-                   
 
-                     for(var i = 0;i<interfaceInstance.methods.length;i++){  
 
-                         var methodName = interfaceInstance.methods[i];  
 
-                         if( !object[methodName] || typeof object[methodName] !==‘function‘){  
 
-                             throw new Error(methodName+‘不是函数或没有被实现‘);  
 
-                         }  
 
-                     }  
 
-                 }  
 
-             }  
 
-               
 
-             var c1 = new MyImpl();  
 
-                 Interface.ensureImplements(c1,CompositeInterface,FormItemInterface);  
 
-                 c1.add();  
 
-         </script>  
 
-     </head>  
 
-     <body>  
 
-     </body>  
 
- </html>  
 

 
 
javascript鸭式辩型法实现接口
原文:http://www.cnblogs.com/zhangxinerfenxiang/p/5870277.html