zoukankan      html  css  js  c++  java
  • javascript鸭式辩型法实现接口

    [html] 
     
    1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
    2. "http://www.w3.org/TR/html4/loose.dtd">  
    3. <html xmlns="http://www.w3.org/1999/xhtml">  
    4.     <head>  
    5.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    6.         <script charset="UTF-8" type="text/javascript">  
    7.             /**  
    8.              * 创建接口类  
    9.              */  
    10.               
    11.             function Interface(name,methods){  
    12.                 if(arguments.length<2){  
    13.                     throw new Error('需要传递两个参数');  
    14.                 }  
    15.                 this.name = name;  
    16.                 this.methods = [];  
    17.                 for(var i = 1;i<methods.length;i++){  
    18.                     var methodName = methods[i];  
    19.                     if(typeof methodName !=='string'){  
    20.                         throw new Error('方法要为字符串类型');  
    21.                     }  
    22.                     this.methods.push(methodName);  
    23.                 }  
    24.             }  
    25.               
    26.             var CompositeInterface = new Interface('CompositeInterface',['add','remove']);  
    27.             var FormItemInterface = new Interface('FormItemInterface',['select','update']);  
    28.             /**  
    29.              * 创建实现类  
    30.              */  
    31.             function MyImpl(){  
    32.                   
    33.             }  
    34.               
    35.             /**  
    36.              * 实现接口  
    37.              */  
    38.             MyImpl.prototype.add = function(){  
    39.                 alert('add...');  
    40.             }  
    41.               
    42.             MyImpl.prototype.remove = function(){  
    43.                 alert('remove...');  
    44.             }  
    45.             MyImpl.prototype.select = function(){  
    46.                 alert('select...');  
    47.             }  
    48.             // MyImpl.prototype.update = function(){  
    49.                 // alert('update...');  
    50.             // }  
    51.               
    52.             /**  
    53.              * 检测是否实现接口中的所有方法  
    54.              */  
    55.             Interface.ensureImplements = function(object){  
    56.                 if(arguments.length<2){  
    57.                     throw Error('参数个数不能小于2个');  
    58.                 }  
    59.                 for(var j=1;j<arguments.length;j++){  
    60.                     var interfaceInstance = arguments[j];  
    61.                     if(interfaceInstance.constructor !==Interface){  
    62.                         throw new Error(interfaceInstance+'不是所需接口实例');  
    63.                     }  
    64.                   
    65.                   
    66.                     for(var i = 0;i<interfaceInstance.methods.length;i++){  
    67.                         var methodName = interfaceInstance.methods[i];  
    68.                         if( !object[methodName] || typeof object[methodName] !=='function'){  
    69.                             throw new Error(methodName+'不是函数或没有被实现');  
    70.                         }  
    71.                     }  
    72.                 }  
    73.             }  
    74.               
    75.             var c1 = new MyImpl();  
    76.                 Interface.ensureImplements(c1,CompositeInterface,FormItemInterface);  
    77.                 c1.add();  
    78.         </script>  
    79.     </head>  
    80.     <body>  
    81.     </body>  
    82. </html>  

     

  • 相关阅读:
    海康、大华摄像头RTSP接入实现WEB端无插件流媒体服务EasyNVR实现海康大华宇视摄像头内容网页播放的方法
    【 D3.js 高级系列 — 6.0 】 值域和颜色
    物联网操作系统HelloX V1.78测试版正式发布
    【 D3.js 高级系列 — 5.1 】 颜色插值和线性渐变
    清华尹成老师主办的智锋互联
    【 随笔 】 D3 难吗?
    禅道,然之和蝉知入驻VM Depot
    【 D3.js 高级系列 — 5.0 】 颜色
    尹成老师主办培训教学机构 智锋互联 传播智慧我们用心,人生冲锋你们用心
    体验Azure的 Automation “自动化” 服务预览版
  • 原文地址:https://www.cnblogs.com/zhangxinerfenxiang/p/5870277.html
Copyright © 2011-2022 走看看