JavaScript 实现接口 (Interfaces In JavaScript)
接口是面向对象编程里的重要特性,遗憾的是JavaScript并没有提供对接口的支持!怎么实现接口呢?
在实际中,我们可以在注释中定义好接口,在实际的代码中予以实现
比如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /* 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 () { ... }; |
实现接口的程序员是否将这些接口都实现了呢?我们没办法保证!因为这里没有任何办法去检查是否都实现了
我们需要一个检查是否实现了接口的机制,可以这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | /* 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方法!因此,我们需要检验实现接口的类是否包含了接口里的方法!所以,接口必须从注释中解放出来:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // 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的定义:两个参数,第一个参数是接口名称,第二个参数是接口包含的方法数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // 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 添加建议接口是否实现的静态方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // 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." ); } } } }; |
关注作者
作者: JadePeng
出处:https://www.cnblogs.com/xiaoqi/archive/2010/06/09/1755084.html
版权:本文采用「署名-非商业性使用-相同方式共享 4.0 国际(欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接) 」知识共享许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了