JavaScript接口實(shí)現(xiàn)代碼 (Interfaces In JavaScript)
更新時(shí)間:2010年06月11日 00:30:21 作者:
接口是面向?qū)ο缶幊汤锏闹匾匦?,遺憾的是JavaScript并沒(méi)有提供對(duì)接口的支持!怎么實(shí)現(xiàn)接口呢?
在實(shí)際中,我們可以在注釋中定義好接口,在實(shí)際的代碼中予以實(shí)現(xiàn)
比如:
實(shí)現(xiàn)接口的程序員是否將這些接口都實(shí)現(xiàn)了呢?我們沒(méi)辦法保證!因?yàn)檫@里沒(méi)有任何辦法去檢查是否都實(shí)現(xiàn)了
我們需要一個(gè)檢查是否實(shí)現(xiàn)了接口的機(jī)制,可以這樣:
這種方法讓程序員在寫(xiě)的時(shí)候注明實(shí)現(xiàn)了哪些接口: this.implementsInterfaces = ['Composite', 'FormItem']; 在調(diào)用的時(shí)候使用implements方法來(lái)判斷是否實(shí)現(xiàn)了,理論上可行,很有可能寫(xiě)上了實(shí)現(xiàn)了'Composite'接口,但是代碼里卻并沒(méi)有add方法!因此,我們需要檢驗(yàn)實(shí)現(xiàn)接口的類是否包含了接口里的方法!所以,接口必須從注釋中解放出來(lái):
定義接口Composite,F(xiàn)ormItem,并且CompositeForm實(shí)現(xiàn)這兩個(gè)接口,在使用的時(shí)候,用Interface.ensureImplements來(lái)檢驗(yàn)formInstance是否實(shí)現(xiàn)了這兩個(gè)接口中的所有方法。
來(lái)看看Interface的定義:兩個(gè)參數(shù),第一個(gè)參數(shù)是接口名稱,第二個(gè)參數(shù)是接口包含的方法數(shù)組
為Interface 添加建議接口是否實(shí)現(xiàn)的靜態(tài)方法
比如:
復(fù)制代碼 代碼如下:
/*
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) { // 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() {
...
};
實(shí)現(xiàn)接口的程序員是否將這些接口都實(shí)現(xiàn)了呢?我們沒(méi)辦法保證!因?yàn)檫@里沒(méi)有任何辦法去檢查是否都實(shí)現(xiàn)了
我們需要一個(gè)檢查是否實(shí)現(xiàn)了接口的機(jī)制,可以這樣:
復(fù)制代碼 代碼如下:
/*
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.
}
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.
}
這種方法讓程序員在寫(xiě)的時(shí)候注明實(shí)現(xiàn)了哪些接口: this.implementsInterfaces = ['Composite', 'FormItem']; 在調(diào)用的時(shí)候使用implements方法來(lái)判斷是否實(shí)現(xiàn)了,理論上可行,很有可能寫(xiě)上了實(shí)現(xiàn)了'Composite'接口,但是代碼里卻并沒(méi)有add方法!因此,我們需要檢驗(yàn)實(shí)現(xiàn)接口的類是否包含了接口里的方法!所以,接口必須從注釋中解放出來(lái):
復(fù)制代碼 代碼如下:
// 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.
...
}
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,F(xiàn)ormItem,并且CompositeForm實(shí)現(xiàn)這兩個(gè)接口,在使用的時(shí)候,用Interface.ensureImplements來(lái)檢驗(yàn)formInstance是否實(shí)現(xiàn)了這兩個(gè)接口中的所有方法。
來(lái)看看Interface的定義:兩個(gè)參數(shù),第一個(gè)參數(shù)是接口名稱,第二個(gè)參數(shù)是接口包含的方法數(shù)組
復(fù)制代碼 代碼如下:
// 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]);
}
};
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 添加建議接口是否實(shí)現(xiàn)的靜態(tài)方法
復(fù)制代碼 代碼如下:
// 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.");
}
}
}
};
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.");
}
}
}
};
相關(guān)文章
JavaScript 類的定義和引用 JavaScript高級(jí)培訓(xùn) 自定義對(duì)象
在Java語(yǔ)言中,我們可以定義自己的類,并根據(jù)這些類創(chuàng)建對(duì)象來(lái)使用,在Javascript中,我們也可以定義自己的類,例如定義User類、Hashtable類等等。2010-04-04最簡(jiǎn)單的javascript對(duì)象實(shí)例代碼
非常簡(jiǎn)單的js面向?qū)ο髮?shí)例代碼,主要是利用了this,對(duì)于js面向?qū)ο蟮膶W(xué)習(xí)資料,可以查看腳本之家以前的文章。2009-12-12javascript中的對(duì)象創(chuàng)建 實(shí)例附注釋
為了讓你的js代碼更加的專業(yè)與代碼的條理性,很多情況下都是定義成對(duì)象的方式來(lái)書(shū)寫(xiě)代碼,想深入的朋友可以參考下。2011-02-02javascript中類的定義及其方式(《javascript高級(jí)程序設(shè)計(jì)》學(xué)習(xí)筆記)
javascript也是一種面向?qū)ο蟮木幊陶Z(yǔ)言。但是javascript中的類相關(guān)的東西(類的定義,原型鏈,繼承等)卻不是很好理解,特別是繼承。2011-07-07JavaScript 設(shè)計(jì)模式 安全沙箱模式
沙箱模式常見(jiàn)于YUI3 core,它是一種采用同一構(gòu)造器(Constructor)生成彼此獨(dú)立且互不干擾(self-contained)的實(shí)例對(duì)象,而從避免污染全局對(duì)象的方法2010-09-09