zoukankan      html  css  js  c++  java
  • javascript 实现 接口编程

    // Constructor.
    var Interface = function (name, methods) {
            if (arguments.length != 2) {
                throw new Error("Interface constructor called with " + arguments.length + "arguments, but expected exactly 2.");
            }
            this.name = name;
            this.methods = [];
            for (var i = 0, len = methods.length; i < len; i++) {
                if (typeof methods[i] !== 'string') {
                    throw new Error("Interface constructor expects method names to be " + "passed in as a string.");
                }
                this.methods.push(methods[i]);
            }
        };
     
     
    // Static class method.
    Interface.ensureImplements = function (object) {
        if (arguments.length < 2) {
            throw new Error("Function Interface.ensureImplements called with " + arguments.length + "arguments, but expected at least 2.");
        }
        for (var i = 1, len = arguments.length; i < len; i++) {
            var interface = arguments[i];
            if (interface.constructor !== Interface) {
                throw new Error("Function Interface.ensureImplements expects arguments" + "two and above to be instances of Interface.");
            }
            for (var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
                var method = interface.methods[j];
                if (!object[method] || typeof object[method] !== 'function') {
                    throw new Error("Function Interface.ensureImplements: object " + "does not implement the " + interface.name + " interface. Method " + method + " was not found.");
                }
            }
        }
    };
    

      

    //定义了一个USB接口
    var USB = new Interface('USB', ['read', 'write']);
    
    //FlashDisk类,需要实现USB接口
    function FlashDisk() {
    }
    
    FlashDisk.prototype.read = function () {
    };
    
    FlashDisk.prototype.write = function () {
    };
    
    //MP3类,同样需要实现USB接口
    function MP3() {
    }
    
    MP3.prototype.read = function () {
    };
    
    MP3.prototype.write = function () {
    };
    
    //没有实现USB接口或没有全部实现
    function BadDevice() {
    }
    
    BadDevice.prototype.write = function () {
    };
    
    //没有实现该接口
    /*BadDevice.prototype.read = function () {
    }*/
    
    //USB设备管理类
    function USBManager() {
      this.devices = [];
    }
    
    USBManager.prototype.add = function (device) {
      Interface.ensureImplements(device, USB); //确保添加的设备实现了USB接口,没有实现的话会抛出异常
    
      this.devices.push(device);
    };
    
    var disk = new FlashDisk();
    var mp3 = new MP3();
    var bad = new BadDevice();
    
    var manager = new USBManager();
    
    manager.add(disk);
    manager.add(mp3);
    
    manager.add(bad); //此处会抛出异常

    接口是基于实例方法的,如果未实现,静态方法就会抛出错误。

  • 相关阅读:
    极光推送的设备唯一性标识 RegistrationID
    排行榜算法设计实现比较 排序树 平衡二叉树
    UCloud首尔机房整体热迁移是这样炼成的
    from appium import webdriver 使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium)
    客户续费模型 逻辑回归 分类器 AdaBoost
    推举算法 AdaBoost 哥德尔奖 Godel Prize
    基于 redis 的分布式锁实现 Distributed locks with Redis debug 排查错误
    Django’s cache framework
    随机森林算法预测法官判决
    时间特征正弦化
  • 原文地址:https://www.cnblogs.com/hualiu0/p/5340810.html
Copyright © 2011-2022 走看看