-
Notifications
You must be signed in to change notification settings - Fork 3
Interfaces
PhoenixAndMachine edited this page Apr 7, 2014
·
17 revisions
Java has keywords Interface and implements to make Interfacing possible. But javascript has no such things. So according to the definition or the purpose of Interface functionality, there are several points we need to fulfill.
-
Defining Interface
Interface should have a name at least, and a set of methods, which are required to be implemented by those classes/objects.
-
Checking whether class/object implements certain interface
There should be an approach to check whether class/object have implemented those methods defined in the interface it declares to implement.
Let’s define an interface in a file called interface.js.
/* Emulate interfaces with duck typing */
/* Define interface */
var Interface = function(name, methods) {
/*
name: the name of interface
methods: the methods which interface defines
*/
if(arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length +
" arguments, but expected 2.");
}
this.name = name;
this.methods = [];
for(var i=0, length=methods.length; i<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]);
}
};
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, length=arguments.length; i<length; i++) {
var interface = arguments[i];
if(interface.constructor !== Interface) {
throw new Error("Interface.ensureImplements expects instance of Interface.");
}
for(var j=0, method_length=interface.methods.length; j<method_length; j++) {
var method_name = interface.methods[j];
//console.log(method_name);
if(!object[method_name] || typeof object[method_name] !== 'function') {
throw new Error("Function.ensureImplements: object does not implement the " +
interface.name + " interface. Method " + method_name + " was not found.");
}
}
}
};
module.exports = Interface;Interface = require('./Interface');
EmotionalBot = new Interface('EmotionalBot', ['cry', 'smile', 'laugh', 'being_jealous']);
RationalBot = new Interface('RationalBot', ['calculate', 'reasoning']);
var SmartBot = function(name, gender) {
this.name = name;
this.gender = gender;
}
var robot = new SmartBot('Sheldon', 'ladyboy');
function action(object) {
try {
console.log("User Interface.ensureImplements can ensure " +
"the object has implemented methods from interfaces.")
Interface.ensureImplements(object, EmotionalBot, RationalBot);
object.cry();
object.calculate();
object.smile();
object.reasoning();
object.being_jealous();
}
catch(err) {
console.log(err);
}
}
action(robot);
SmartBot.prototype.cry = function() {
console.log(this.name+" is crying.");
};
SmartBot.prototype.smile = function() {
console.log(this.name+" is smile.");
};
SmartBot.prototype.laugh = function() {
console.log(this.name+" is laughing.");
};
SmartBot.prototype.being_jealous = function() {
console.log(this.name+" is jealous.");
};
SmartBot.prototype.calculate = function() {
console.log(this.name+" can calculate.");
};
SmartBot.prototype.reasoning = function() {
console.log(this.name+" can do reasoning.");
};
action(robot);