日期:2014-05-16 浏览次数:20318 次
?
在JavaScript中模仿接口——本文摘自《JavaScript设计模式》
?
一、用注释描述接口
?
/* interface Composite { function add(child); function remove(child); function getChild(index); } interface FormItem { function save(); } */ var CompositeForm = function(id, method, action){ //...... }; //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(){ //...... }?
这种模仿并不是很好,他们有为确保CompositeForm真正实现正确的方法集而进行检查,
也不会跑出错误以高质程序员程序中的问题。
?
二、用属性检查模仿接口
?
/* 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 delcares that it //implements the required interfaces. function implements(object){ for(var i = 0; 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; //An interface was not found. } } return true; //All interface were found. }?
在这个例子中,CompositeForm宣传自己实现了Composite和FormItem这两个接口,其做法是吧这链各个接口的
名称加入一个名为implement上Interfaces的数组。
?
三、用鸭式辨型模仿接口
//Interfaces
var composite = new Interface('Composite', ['add', 'remove', 'getChild']); var FormItem = new Interface('FormItem', ['save']); //CompositeForm class var CompositeForm = function(id, method, action){ //........ } function addForm(formInstancd){ ensureImplements(formInstance, Composite, FormItem); //........ }?
待续……