接口
在JavaScrip中模仿接口
用注释描述接口
/*
interface Composite {
function add(child);
function remove(child);
function getChild(index);
}
interface FormItem {
function save();
}
*/
var Composite = function(id, method, action) { // implements Composite, FormItem
};
Composite.prototype.add = function(child) {
console.log("add");
}
Composite.prototype.remove = function(child) {
console.log("remove");
}
Composite.prototype.getChild = function(index) {
console.log("getChild");
}
Composite.prototype.save = function() {
console.log("save");
};
var instance = new Composite(1, 2, 3);
instance.add();
instance.remove();
instance.getChild();
instance.save();
用属性检查模仿接口
概括:
- 一个方法用于表明自己实现了哪些接口。
- 一个方法用于验证是否实现了所有的接口,如果没有,则抛出异常。
- 一个方法用于判断某个类是否实现了所有的接口。
缺点:
- 自己说自己实现了什么接口,但是可能自己并没有实现,但是代码却不会抛出异常。
- 用了两层for循环,会引起性能消耗。
/*
interface Composite {
function add(child);
function remove(child);
function getChild(index);
}
interface FormItem {
function save();
}
*/
var CompositeForm = function(id, method, action) {
this.implementsInterface = ['Composite', 'FormItem'];
// ...
}
function addForm(instance) {
if(!implements(instance, 'Composite', 'FormItem')) {
throw new Error('Object does not implement a required interface');
}
if(!implements(instance, 'test', 'FormItem')) {
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.implementsInterface.length;j++) {
if(object.implementsInterface[j] == interfaceName) {
interfaceFound = true;
break;
}
}
if(!interfaceFound) {
return false;
}
}
return true;
}
var instance = new CompositeForm(1, 2, 3);
addForm(instance);
用鸭式辨型模仿接口
- 类是否声明自己支持哪些接口并不重要, 只要具有这些接口中的方法就可以了。
- 它把对象实现的方法集作为判断它是不是某个类的实例的唯一标准。
- 也就是说,如果对象具有与接口定义的方法同名的所有方法,那么就可以认为它实现了这个接口。
// Interface.js文件
/*
* 一个接口
* @param {string} name 接口的名字
* @param {array} methods 接口要实现的方法
* @class
*/
var Interface = function(name, methods) {
if(arguments.length !== 2) {
throw new Error("Interface constructor called with " + arguments.length
+ "arguments, but expectly exactly 2.");
}
this.name = name;
this.methods = [];
for(var i = 1;i < methods.length;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 用于实现检测对象是否实现了接口里的所有方法
*
* @param {object} object 它应该包含一个对象的实例和至少一个接口
*
*/
Interface.prototype.ensureImplements = function(object) {
if(arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with " + arguments.length
+ "arguments, but expectly at least 2.");
}
for(var i = 1;i < arguments.length;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");
}
}
}
}
// index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="Interface.js"></script>
<script type="text/javascript">
// 实例化一个接口
var PeopleInterface = new Interface('PeopleInterface', ['run', 'eat']);
// 封装一个People类
var People = function(name) {
this.name = name;
};
People.prototype.run = function() {
console.log(this.name + " is running...");
};
People.prototype.eat = function() {
console.log(this.name + " is eating...");
};
// 实例化一个People对象
var yzf = new People('yzf');
function addPeople(instance) {
// 对yzf进行检测,看它是否完全继承了PeopleInterface接口
PeopleInterface.ensureImplements(yzf, PeopleInterface);
// 如果没有抛出异常,就可以使用下面这些方法了
yzf.run();
yzf.eat();
}
addPeople(yzf);
</script>
</body>
</html>