Created
July 8, 2013 19:38
-
-
Save battlejj/5951832 to your computer and use it in GitHub Desktop.
A buddy of mine coming from the Java world wondered if JS had any implementation of interfaces. I told him no but tried to come up with a way to make it work. Removing any required method from the prototypes of either object that inherits from UserDAO will throw an error, but you can still verify the instance of each Object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var inherits = function(ctor, superCtor) { | |
ctor.super_ = superCtor; | |
ctor.prototype = Object.create(superCtor.prototype, { | |
constructor: { | |
value: ctor, | |
enumerable: false, | |
writable: true, | |
configurable: true | |
} | |
}); | |
}; | |
var UserDAO = function(type){ | |
if(type && type == "PG") { | |
return new UserPGDAO() | |
} else { | |
return new UserMySQLDAO() | |
} | |
} | |
UserDAO.prototype.initInterface = function(){ | |
var mustImplement = [ | |
{ | |
method: 'getUser', | |
args: ['id'] | |
}, | |
{ | |
method: 'getUsers', | |
args: [] | |
}, | |
{ | |
method: 'deleteUser', | |
args: [] | |
}, | |
{ | |
method: 'updateUser', | |
args: [] | |
} | |
] | |
for(interface in mustImplement){ | |
if(Object.getPrototypeOf(this).hasOwnProperty(mustImplement[interface].method) | |
&& typeof Object.getPrototypeOf(this)[mustImplement[interface].method] == 'function' | |
&& Object.getPrototypeOf(this)[mustImplement[interface].method].length == mustImplement[interface].args.length){ | |
console.log('Implements ' + mustImplement[interface].method + ' correctly.') | |
} else { | |
console.log('Does not implement ' + mustImplement[interface].method + ' correctly.') | |
throw('Does not implement ' + mustImplement[interface].method + ' correctly.') | |
} | |
} | |
} | |
var UserMySQLDAO = function(){ | |
this.initInterface() | |
return this | |
} | |
inherits(UserMySQLDAO, UserDAO) | |
UserMySQLDAO.prototype.getUsers = function(){ | |
console.log('get mysql users') | |
} | |
UserMySQLDAO.prototype.getUser = function(id){ | |
console.log('get mysql user by id') | |
} | |
UserMySQLDAO.prototype.deleteUser = function(){ | |
console.log('delete mysql users') | |
} | |
UserMySQLDAO.prototype.updateUser = function(){ | |
console.log('update mysql users') | |
} | |
var UserPGDAO = function(){ | |
this.initInterface() | |
return this | |
} | |
inherits(UserPGDAO, UserDAO) | |
UserPGDAO.prototype.getUsers = function(){ | |
console.log('get pg users') | |
} | |
UserPGDAO.prototype.getUser = function(id){ | |
console.log('get pg user by id') | |
} | |
UserPGDAO.prototype.deleteUser = function(){ | |
console.log('delete pg users') | |
} | |
UserPGDAO.prototype.updateUser = function(){ | |
console.log('update pg users') | |
} | |
var userDAO1 = new UserDAO("PG"); | |
var userDAO2 = new UserDAO("MySQL"); | |
console.log(userDAO1 instanceof UserPGDAO); | |
console.log(userDAO1 instanceof UserMySQLDAO); | |
console.log(userDAO2 instanceof UserPGDAO); | |
console.log(userDAO2 instanceof UserMySQLDAO); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment