JavaScript接口实现代码 (Interfaces In JavaScript)

在实际中,我们可以在注释中定义好接口,在实际的代码中予以实现
比如:


代码如下:

/*
interface Composite {
function add(child);
function remove(child);
function getChild(index);
}
interface FormItem {
function save();
}
*/
var CompositeForm = function(id, method, action) { // implements Composite, FormItem
...
};
// Implement the Composite interface.
CompositeForm.prototype.add = function(child) {
...
};
CompositeForm.prototype.remove = function(child) {
...
};
CompositeForm.prototype.getChild = function(index) {
...
};
// Implement the FormItem interface.
CompositeForm.prototype.save = function() {
...
};

实现接口的程序员是否将这些接口都实现了呢?我们没办法保证!因为这里没有任何办法去检查是否都实现了
我们需要一个检查是否实现了接口的机制,可以这样:


代码如下:

/*
interface Composite {
function add(child);
function remove(child);
function getChild(index);
}
interface FormItem {
function save();
}
*/
var CompositeForm = function(id, method, action) {
this.implementsInterfaces = ['Composite', 'FormItem'];
...
};
...
function addForm(formInstance) {
if(!implements(formInstance, 'Composite', 'FormItem')) {
throw new Error("Object does not implement a required interface.");
}
...
}
// The implements function, which checks to see if an object declares that it
// implements the required interfaces.
function implements(object) {
for(var i = 1; i < arguments.length; i++) { // Looping through all arguments
// after the first one.
var interfaceName = arguments[i];
var interfaceFound = false;
for(var j = 0; j < object.implementsInterfaces.length; j++) {
if(object.implementsInterfaces[j] == interfaceName) {
interfaceFound = true;
break;
}
}

if(!interfaceFound) {
return false; // An interface was not found.
}
}
return true; // All interfaces were found.
}

这种方法让程序员在写的时候注明实现了哪些接口: this.implementsInterfaces = ['Composite', 'FormItem']; 在调用的时候使用implements方法来判断是否实现了,理论上可行,很有可能写上了实现了'Composite'接口,但是代码里却并没有add方法!因此,我们需要检验实现接口的类是否包含了接口里的方法!所以,接口必须从注释中解放出来:


代码如下:

// Interfaces.
var Composite = new Interface('Composite', ['add', 'remove', 'getChild']);
var FormItem = new Interface('FormItem', ['save']);
// CompositeForm class
var CompositeForm = function(id, method, action) { // implements Composite, FormItem
...
};
...
function addForm(formInstance) {
Interface.ensureImplements(formInstance, Composite, FormItem);
// This function will throw an error if a required method is not implemented,
// halting execution of the function.
// All code beneath this line will be executed only if the checks pass.
...
}

定义接口Composite,FormItem,并且CompositeForm实现这两个接口,在使用的时候,用Interface.ensureImplements来检验formInstance是否实现了这两个接口中的所有方法。
来看看Interface的定义:两个参数,第一个参数是接口名称,第二个参数是接口包含的方法数组


代码如下:

// 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]);
}
};

为Interface 添加建议接口是否实现的静态方法


代码如下:

// Constructor.
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.");
}
}
}
};

(0)

相关推荐

  • PHP abstract与interface之间的区别

    1.php 接口类:interface 其实他们的作用很简单,当有很多人一起开发一个项目时,可能都会去调用别人写的一些类,那你就会问,我怎么知道他的某个功能的实现方法是怎么命名的呢,这个时候php接口类就起到作用了,当我们定义了一个接口类时,它里面的方式是下面的子类必须实现的,比如 : 复制代码 代码如下: interface Shop {       public function buy($gid);       public function sell($gid);       publi

  • java abstract class interface之间的区别介绍

    含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必是抽象的.abstract class类中定义抽象方法必须在具体(Concrete)子类中实现,所以,不能有抽象构造方法或抽象静态方法.如果的子类没有实现抽象父类中的所有抽象方法,那么子类也必须定义为abstract类型. 接口(interface)可以说成是抽象类的一种特例,接口中的所有方法都必须

  • java interface的两个经典用法

    本文为大家分享了java interface的两个经典用法,供大家参考,具体内容如下 1.Java多态接口动态加载实例 编写一个通用程序,用来计算没一种交通工具运行1000公里所需的时间,已知每种交通工具的参数都为3个整数A.B.C的表达式.现有两种工具:Car和Plane,其中Car的速度运算公式为:A+B+C.需要编写三个类:ComputeTime.java,Palne.java,Car.java和接口Common.java.要求在未来如果增加第3中交通工具的时候,不必修改 以前的任何程序,

  • Go语言interface详解

    interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单的说,interface是一组method的组合,我们通过interface来定义对象的一组行为. 我们前面一章最后一个例子中Student和Employee都能SayHi,虽然他们的内部实现不一样,但是那不重要,重要的是他们都能say hi 让我们来继续做更多的扩展,Student和Employe

  • AS3 navigateToURL导致ExternalInterface 执行失败问题

    我们先看下面代码: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <!--[CDATA[ import flash.external.ExternalInt

  • c#中的interface abstract与virtual介绍

    interface用来声明接口1.只提供一些方法规约,不提供方法主体.  如: 复制代码 代码如下: public interface IPerson{    void getName();//不包含方法主体} 2.方法不能用public abstract等修饰,无字段变量,无构造函数.3.方法可包含参数.  如 复制代码 代码如下: public interface IPerson  {    void getAge(string s);  } 一个例子(例1): 复制代码 代码如下: pub

  • C#接口(Interface)用法分析

    本文实例分析了C#接口(Interface)用法.分享给大家供大家参考.具体分析如下: 继承"基类"跟继承"接口"都能实现某些相同的功能,但有些接口能够完成的功能是只用基类无法实现的 1.接口用于描述一组类的公共方法/公共属性. 它不实现任何的方法或属性,只是告诉继承它的类至少要实现哪些功能,继承它的类可以增加自己的方法. 2.使用接口可以使继承它的类: 命名统一/规范,易于维护.比如: 两个类 "狗"和"猫",如果它们都继承

  • 领悟php接口中interface存在的意义

    可能大家都懂这些,作为不懂的我猜测了一下这个interface的意义,他就是为了后面调用的时候再调用的方法中调用实现类中interface中存在的内容,好绕口啊,写个例子留作以后看吧pay.php 复制代码 代码如下: interface Ipay{ function withmoney(); //function withinternet();}class Dmeng implements Ipay{ function withmoney() {  echo "花人民币买东西"; }

  • c# 接口interface基础入门小例子

    复制代码 代码如下: /// <summary>    /// interface    /// 与抽象类的区别:    /// 1,abstract可以有具体方法和抽象方法(必须有一个抽象方法),interface没有方法实现    /// 2,abstract可以有构造函数和析构函数,接口不行    /// 3,一个类可以实现多个interface,但只能继承一个abstract    /// 特点:    /// interface成员隐式具有public,所以不加修饰符    ///

  • C#中接口(interface)的理解

    .都是"虚的"不能被实例化,这也是接口中为什么不能包含字段--成员变量的原因. 2.正因为接口是虚的,所以接口内的索引,属性,时间等只能有声明,而不能在接口内实现,具体如何实现是派生接口或者派生类的事. 3.都具有模板的性质,如果一个接口或者类从某一个接口继承,它将自动具有被集成者的特征(包括索引,属性,函数,实践等). 4.接口支持多重继承,而C#中,类之支持单一继承,接口实际表示的是一种承载能力. 下面是接口的一个简单定义: 复制代码 代码如下: interface  SampIn

随机推荐