JavaScript接口

JavaScript中实现接口的方法有三种:

第一种,使用注释的方法实现接口

特点:
(1)最简单,但是功能最弱

(2)利用 interface和 implement"文字"
(3)把他们用注释的方式表现出来

具体实现如下:

1,用注释定义一个接口
 /*
    * interface PersonDao(){
    * function add(obj);
    * function remove(obj);
    * function find(id);
    * }
    * */

(2)用注释来注明实现的接口

 /*
    * PersonDaoImp  implement PersonDao (PersonDaoImp实现接口PersonDao) 
    * */
    var PersonDaoImp=function () {   };//定义实现类
//实现 PersonDaoImp.prototype.add
=function(obj){ //具体代码 } PersonDaoImp.prototype.remove=function(obj){ //具体代码 } PersonDaoImp.prototype.find=function(id){ //具体代码 }

总结:

(1)使用文字的形式告知是谁实现谁
(2)优点,这样是很有意义的,大型项目需要的就是规范和标准,可以在没有写实现之前充分考虑架构和设计
(3)缺点:需要人为的遵守注释中的说明

第二种,使用属性检验法实现接口 。 实质为通过一个属性判断实现了谁
具体如下:

1,用注释来定义一个接口
  /*
     * interface PersonDao(){
     * function add(obj);
     * function remove(obj);
     * function find(id);
     * }
     * */
2,用注释来说明实现接口类+实现类中增加属性
/*
     * PersonDaoImp  implement PersonDao
     * */
    var PersonDaoImp=function () {
        this.implementInterface=["PersonDao"];//告知该类实现的接口是啥是一个数组,
}
    PersonDaoImp.prototype.add=function(obj){
             alert(obj);
    }
    PersonDaoImp.prototype.remove=function(obj){
        //具体实现
    }
    PersonDaoImp.prototype.find=function(id){
        //具体实现
    }

(3)检验属性的方法

  //接收一个不定参数 可能有多个  使用Object
        function imp1(Object) {
        //遍历传入对象的所用属性  i=1:第一个是不定参数,从第二个参数开始遍历接口,故i=1
            for(var i=1;i<arguments.length;i++){//arguments除Object外
                var interfaceName=arguments[i];
                var interfaceFind=false;
                for(var j=0;j<Object.implementInterface.length;j++){
                     if(Object.implementInterface[j]==interfaceName){
                         interfaceFind=true;
                         break;
                     }
                }
                if(!interfaceFind){
                    return false;
                }
            }
            return true;
        }

(4)接口与实现类的配合实现

  function addObj(obj) {
        var PersonDao=new PersonDaoImp();
        //开始检查  实现类是否实现接口
            if(!imp1(PersonDao,"PersonDao")){//某对象是否实现接口(对象,接口)   第一次参数是对象,第二个参数是不定参数
                throw  new Error("PersonDaoImp没有实现接口PersonDao");
            }else{//实现
                PersonDao.add(obj);
            }
    }

(5)使用

addObj("实现");

 总结一下,该种方式只是简单判断了在实现时有没有传递与属性中相同的接口名称,而对于方法是否实现没有做验证。

 于是有了第三种的鸭式变形法--检验接口中的方法是否实现。
第三种,鸭式变形法  一种形似的命名方式,从实现角度来理解为:如果对象中具有的方法与接口中定义的方法同名  则认为是实现了本接口。
具体如下:
1,定义一个接口类 注意这里与上面两种不一样了,不用写注释说明了
var Interface=function (name,methods) {//name:接口名字
        if(arguments.length<2){
            alert("必须是两个参数")
        }
        this.name=name;
        this.methods=[];//定义一个空数组装载函数名
        for(var i=0;i<methods.length;i++){
            if(typeof  methods[i]!="string"){
                alert("函数名必须是字符串类型");
            }else {
                this.methods.push( methods[i]);
            }
        }
    }

2,定义一个静态方法来实现接口与实现类的 直接检验
注意,
静态方法不要写成Interface.prototype ,因为这是写到接口的原型链上的,我们要把静态的函数直接写到类层次上。
Interface.ensureImplement=function (object) {
     if(arguments.length<2){
         throw  new Error("参数必须不少于2个")
         return false;
     }
     for(var i=1;i<arguments.length;i++){
         var inter=arguments[i];
         //如果是接口就必须是Interface类型
         if(inter.constructor!=Interface){
                throw  new Error("如果是接口类的话,就必须是Interface类型");
         }
            //判断接口中的方法是否全部实现
           //遍历函数集合
          for(var j=0;j<inter.methods.length;j++){
            var method=inter.methods[j];//接口中所有函数

              //object[method]是传入的函数
             if(!object[method]||typeof object[method]!="function" ){//实现类中必须有方法名字与接口中所用方法名相同
                     throw  new Error("实现类中没有完全实现接口中的所有方法")
                        }
                    }
     }
 }

3,应用

3.1定义自己的接口    

         例如:此处定义两个接口

 var FirstInterface=new Interface("FirstInterface",["add","remove","search"]);//第一个接口
 var SecondInterface=new Interface("SecondInterface",["save"]);//第二个接口

3.2,定义实现类

 function commManager() {//实现两个类
        //先实现方法
        this.add=function () {
            alert("ok--实现");
        }
        this.remove=function () {
        }
        this.search=function () {
        }
        this.save=function () {
        }
        //检验
        Interface.ensureImplement(this,GridManager,formManager);
    }

3.3,实现类的实例化

var comm=new commManager();
    comm.add();//调用

 总结:三种方式都有自己的优势与缺点,每种的选择需要根据自己的需要进行选择。但是在设计的时候实现类间低耦合的相当重要的。

本人初学,若有不足之处,还希望大家能多多指出,大家相互学习,谢谢。

 




原文地址:https://www.cnblogs.com/wfaceboss/p/7646969.html