// 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); //此处会抛出异常

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

最新文章

  1. jquery.Callbacks的实现
  2. mysql命令
  3. EX:The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
  4. jQuery.makeArray() 函数详解
  5. Hive(六):HQL DDL
  6. 如何获得JVM执行过程中调用的方法名
  7. python之安装
  8. Git基础 1 ---- 版本控制系统的介绍
  9. 简单的http代理服务器
  10. html的表单表格...
  11. linux自动化创建补丁
  12. 00_Linux介绍_我的Linux之路
  13. 【unix网络编程第三版】阅读笔记(二):套接字编程简介
  14. 如何使用Sencha touch 构建基于Cordova的安卓项目
  15. 实现简单的printf函数
  16. odoo11登录之后返回的session信息分析
  17. python第三方库的四种安装方法
  18. 【转】Python 内置函数 locals() 和globals()
  19. PLSQL developer 连接不上64位Oracle 解决办法
  20. ubuntu16.04搭建geodjango+postgresql+postgis的WebGIS框架(一)安装第三方空间库

热门文章

  1. 《TensorFlow实战》中AlexNet卷积神经网络的训练中
  2. 【Python学习之七】面向对象高级编程——使用@property
  3. Ubuntu 下使用虚拟串口进行开发测试
  4. [Python]有关pygame库中的flip和update的区别
  5. 删除项目开发中的.pyc文件
  6. Appium环境搭建及“fn must be a function”问题解决
  7. mysql之面试问题总结
  8. ZeroClipboard_copy
  9. 友推在Android 实现微信等分享代码的常见问题
  10. 引用其他头文件时出现这种错误,莫名其妙,error C2065: “ColorMatrix”: 未声明的标识符