JS接口的实现
发布在随笔2016年11月23日view:6201HTML5YiksiAssow前端开发BrettBat移动开发闭包&作用域前端工程师面试Betti团队协作
在文章任何区域双击击即可给文章添加【评注】!浮到评注点上可以查看详情。

JS接口的实现

实现方法: 1. 注解的方法 2. 属性检验法 3. 鸭式变形法

接口的实现目的: 1. 大型项目提高代码的灵活度 2. 松耦合 3. 在团队开发的时候,有写时候你在真正编码之前就可以写API(自己的类库)

注解的方法

**
 * 最简单,但是功能也是最弱的
 * 他利用inerface和implement"文字"
 * 把他们用注解的方式显示的表现出来
 */
(function(){
    /**
     * 用注释来定义一个接口
     * interface PersonDao(){
     *  function add(obj);
     *  function remove(obj);
     *  function find(id);
     * }
     */
    var PersonDaoImpl = function(){

    }
    PersonDaoImpl.prototype.add = function(obj){
        //..
    }
    PersonDaoImpl.prototype.remove = function(obj){
        //..
    }
    PersonDaoImpl.prototype.find = function(id){
        //..
    }

})()

属性检验法


(function(){

    var PersonDaoImpl = function(){
        this.implementInterface = ["PersonDao"];        
    }
    PersonDaoImpl.prototype.add = function(obj){
        alert(obj)
        //..
    }
    PersonDaoImpl.prototype.remove = function(obj){
        //..
    }
    PersonDaoImpl.prototype.find = function(id){
        //..
    }   
    function addObj(obj){
        var PersonDao = new PersonDaoImpl();
        //开始检查
        if(!impl(PersonDao,"PersonDao")){
            throw new Error("类PersonDaoImpl没有实现接口PersonDao");
        }else{
            PersonDao.add(obj);
        }
    }
    addObj("USCAPT.COM");
    /**
     * 他接受的是一个不定参数
     */
    function impl(Object){
        //遍历出入对象的属性
        for(var i=1;i<arguments.length;i++){
            var interfaceName = arguments[i];
            var interfaceFound = false;
            for(var j=0;j<Object.implementInterface.length;j++){
                if(Object.implementInterface[j] == interfaceName){
                    interfaceFound = true;
                    break;
                }
            }
            if(!interfaceFound){
                return false;
            }
        }
        return true;
    }
})()

鸭式变形法

``` /** * 如果对象具有与接口定义的方法名字的同命所有方法 那么我就认为你就是实现本接口 / (function(){ //定义一个接口类 var Interface = function(name,methods){ if(arguments.length != 2){ alert("interface must have two paramters..."); } this.name = name;//这个是接口的名字 this.methods = [];//定义个空数组来转载函数名 for (var i = 0; i < methods.length; i++) { if(typeof methods[i] != "string"){ alert("method name must is String ...") }else{ this.methods.push(methods[i]) } } } //定义接口的一个静态方法来实现接口与实现类的直接检验 //静态方法不要写成Interface.prototype. 因为这是写到接口原型连上的 //我们要把静态的函数直接写到类层次上 Interface.ensureImplements = function(object){ if(arguments.length<2){ alert("必须最少是2个参数"); return false; } //遍历 for (var i = 1; i < arguments.length; i++) { var inter = arguments[i]; //如果你是接口就必须是Interface类型的 if(inter.constructor != Interface){ throw new Error("if is interface class must is Interface type"); } //遍历函数集合并分析 for (var j = 0; j < inter.methods.length; j++) { var method = inter.methods[j]; //实现类中必须有方法名字 和 接口中所有的方法名项目 if(!object[method] || typeof object[method] != "function"){ throw new Error("实现类并且没有完全实现接口中的所有方法..."); } } } } //应用 //定义自己的接口 var GridMananger = new Interface("GridMananger",["add","remove","list"]); var FormMananger = new Interface("FormMananger",["save"])

function commManager(){
    //先实现方法
    this.add = function(){
        alert("ok")
    }
    this.remove = function(){}
    this.list = function(){}
    this.save = function(){}
    //检验
    Interface.ensureImplements(this,GridMananger,FormMananger)
}
var c = new commManager();
c.add();

})()

评论
发表评论
暂无评论
WRITTEN BY
一诺
水滴石穿不是因为力量 而是因为坚持
TA的新浪微博
PUBLISHED IN

友情链接 大搜车前端团队博客
我的收藏