gr_module_adapter
Version:
gr平台模块适配类库
57 lines (46 loc) • 1.89 kB
JavaScript
/**
* 创建一个模块的基本接口协议
*/
;
// 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]);
}
};
// Static class method.
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 instance = arguments[i];
if (instance.constructor !== Interface) {
throw new Error("Function Interface.ensureImplements expects arguments" + "two and above to be instances of Interface.");
}
for (var j = 0, methodsLen = instance.methods.length; j < methodsLen; j++) {
var method = instance.methods[j];
if (!object[method] || typeof object[method] !== 'function') {
throw new Error("Function Interface.ensureImplements: object " + "does not implement the " + instance.name + " instance. Method " + method + " was not found.");
}
}
}
};
var ModuleAdapter = new Interface('ModuleAdapter',['getMenus','getAcl']);
function ModuleManager(){
this.modules = [];
}
ModuleManager.prototype.plus = function (m) {
Interface.ensureImplements(m, ModuleAdapter);
this.modules.push(m);
};
exports.ModuleAdapter = ModuleAdapter;
exports.ModuleManager = ModuleManager;