摘要

js 语言并没有interface implement 关键字,如果要做到和后端语言类似的接口,一般会需要模拟实现。

在oo 编程中, 接口可以让架构师规定好模块必须要实现的方法, 尽量解耦模块。

实现接口的方法

第一种 注释法 , 只是一种约定,约束太小,而且不能保证接口是否完全实现

/*
interface Composite{
function add(child);
}
interface fromItem{
function save();
}
*/
var CompositeForm = function(id, method, action){ };
CompositeForm.prototype.add = function(child){ };
CompositeForm.prototype.save = function(){ };

第二种方法 属性判断, 虽然可以保证每个接口都实现,但是接口的方法是否实现也没法保证。

/*
interface Composite{
function add(child);
}
interface fromItem{
function save();
}
*/
var CompositeForm = function(){
this.implementsInterfaces = ['Composite', 'FormItem']; //声明自己继承的接口
}; function addForm(formInstance){
if(!implements(formInstance, 'Composite', 'FormItem')){ //检查实例formInstance是否实现了接口
throw new Error('Object does not implement a required interface');
}
}
function implements(object){ //检查算法,双重循环
for(var i=1; i<arguments.length; i++){
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;
}
}
return true;
} addForm(new CompositeForm());

第三种 鸭式辨型模仿接口 , 可以保证 实现每个 接口的方法, 但是引入了 Interface 这些类。

//Constructor
var Interface = function(name, methods){ //将接口的方法保存在this.methods中用以将来的检测
if(arguments.length != 2){
throw new Error('Interface constructor called with '+arguments.legth+
" arguments, but exected 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.ensureImplements = function(object, arguments){
if(arguments.length < 2){
throw new Error('Function Interface.ensureImplements called with'+arguments.length+
"arugments, but expected at least 2.");
}
for (var i=0, len = arguments.length; i<len; i++){
var intf = arguments[i];
if(intf.constructor !== Interface){
throw new Error('Function Interface.ensureImplements expects arguments'+
"two and above to be instances of Interface.");
}
for(var j=0, methodsLen = intf.methods.length; j < methodsLen; j++){
var method = intf.methods[j];
//通过methods检查实例是否实现了接口
if(!object[method] || typeof object[method]!=='function'){
throw new Error('Function Interface.ensureImplements:object'+
" doesnot implement the"+intf.name+
" interface. Method "+method+" was not found!");
}
}
}
}; var Composite = new Interface('Composite', ['add']); //声明接口拥有的方法
var FormItem = new Interface('FormItem', ['save']); //声明接口拥有的方法 var compositeForm = function(id, method, action){//implements Composite, FormItem
};
compositeForm.prototype.add = function () {};
compositeForm.prototype.save = function () {}; function addForm(formInstance){
Interface.ensureImplements.call(this, formInstance, [Composite, FormItem]);
} addForm(new compositeForm());

[js] 实现接口的更多相关文章

  1. html中通过js获取接口JSON格式数据解析以及跨域问题

    前言:本人自学前端开发,一直想研究下js获取接口数据在html的实现,顺利地找到了获取数据的方法,但是有部分接口在调用中出现无法展示数据.经查,发现时跨域的问题,花费了一通时间,随笔记录下过程,以方便 ...

  2. 【转】js生成接口请求参数签名加密

    js生成接口请求参数签名加密 签名算法规则: 第一步,设所有发送或者接收到的数据为集合M,将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),使用URL键值对的格式(即key1=v ...

  3. js生成接口请求参数签名加密

    js生成接口请求参数签名加密 定义规则:将所有参数字段按首字母排序, 拼接成key1 = value1 & key2 = value2的格式,再在末尾拼接上key = appSecret, 再 ...

  4. 记录一次用宝塔部署微信小程序Node.js后端接口代码的详细过程

    一直忙着写毕设,上一次写博客还是元旦,大半年过去了.... 后面会不断分享各种新项目的源码与技术.欢迎关注一起学习哈! 记录一次部署微信小程序Node.js后端接口代码的详细过程,使用宝塔来部署. 我 ...

  5. 项目中angular js的接口url统一管理

    为了防止环境改变时需要修改多处接口的url,项目中用到了一个config.json文件来统一管理url: 在src下建立config文件夹,创建config.json文件,主要内容如下: { &quo ...

  6. 微信js SDK接口

    微信JS-SDK说明文档 http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html 一.微信登录功能 在进行微信OAut ...

  7. js 设计模式-接口

    js模拟java接口检测函数:确保子类实现接口中的方法:(出自js设计模式) 上代码: <script type="text/javascript" > <%-- ...

  8. 如何通过js调用接口

    例如一个接口的返回值如下:var returnCitySN = {"cip": "221.192.178.158", "cid": &quo ...

  9. node.js http接口调试时请求串行特性分析

    缘起: 产品业务上有个类数据库服务的请求时间比较长(类似mysql的sql查询),为了优化减少并发时的请求数,做了一个并发时共用请求的优化. 通过单元测试后,想通过手动模拟看下效果,发现优化一直不能生 ...

随机推荐

  1. Python之道1-环境搭建与pycharm的配置django安装及MySQL数据库配置

    近期做那个python的开发,今天就来简单的写一下开发路线的安装及配置, 开发路线 Python3.6.1+Pycharm5.0.6+Django1.11+MySQL5.7.18 1-安装Python ...

  2. 设计模式(2)工厂方法模式(Factory Method)

    设计模式(0)简单工厂模式 设计模式(1)单例模式(Singleton) 源码地址 0 工厂方法模式简介 0.0 工厂方法模式定义 工厂方法模式是在简单工厂模式基础上,为解决更复杂的对象创建问题而衍生 ...

  3. jqery 图片等 比例缩放

    后台添加加载图片的方法(从编辑器里为图上添加一个名为jzsmsimglightbox样式的类) public string GetHtmlImageUrlList(string sHtmlText) ...

  4. PHPCMS V9 为今天或几天前文章加new

    今天内发布: {pc:content action="lists" catid="13" order="listorder DESC" nu ...

  5. 手动整合实现SSH项目开发01

    内容简介:本文主要介绍SSH项目开发的配置以及简单登录功能的实现. 1. 新建一个Dynamic Web Project. 2.导入需要 的Jar包,此项目是Struts.Hibernate.Spri ...

  6. [leetcode-557-Reverse Words in a String III]

    Given a string, you need to reverse the order of characters in each word within a sentence whilestil ...

  7. C++ 获取文件夹下的所有文件名

    获取文件夹下所有的文件名是常用的功能,今天再一次有这样的需求,所有就在网上查找了很多,并记下以供后用. 原文:http://blog.csdn.NET/cxf7394373/article/detai ...

  8. 【Android Developers Training】 79. 连接到网络

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  9. Mathematica学习笔记2

    导入文件中的矩阵 mat = Import["...", "Table"] 转化为向量矩阵(元素为数对) data = Table[{mat[[i, j]], ...

  10. poj 2299 Ultra-QuickSort 题解

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...