Nov
17
2008
Fully Working Javascript First Class Interfaces Are Possible!
Author: mprokesSo today I am reading through this book pro-javascript design patterns, excellent book by the way! I had come upon chapter 2, which was an excellent explanation of interfaces. One thing that concerned myself is that the book states that it is impossible to create enforceable interfaces in javascript. I pondered that for a bit, as far as what interfaces really are. If you look at interfaces from the perspective of java, then for the most part they are types, with a specified subset of methods for a class instance.
Thinking in those terms, I sat back and pondered the problem again, sooo all I need to do really to create interfaces that are enforceable in javascript would be to separate the concept of an interface from a class all together and make it a class its self! WALA! True first class interfaces in javascript!
So lets see what this new found idea looks like! Here is my interface class that makes it all work.
You can think of interface as linking everything together, but what would a TestInterface look like?
Take note, that the interface its self is already implementing Interface.js in a sort-of way! There are two required methods getInterfaceName() and setUpInterface() that need to be implemented otherwise Interface.js will complain loudly!
The basic idea here is that setUpInterface recieves a closure, that is able to access the implementing object, all the TestInterface needs to do is call that method with the same attributes it recieves. It does this by passing in the name of the method into the closure, and then executing the returned function with the same attributes recieved by the method; and return it, SIMPLE!
You can see this happening here:
return inspector(‘testMethod’)(x,y,z);
So now that we have an interface for our class, what does a javascript class have to do to implement this interface?
The actual implementation of the interface happens here: new Interface(this, new TestInterface());
Wow! thats easy! But what happens when the class doesn’t implement all the methods as this one does?
Well lets find out, if you could place all these scripts in a javascript shell, lets test out a few things.
//Create a new TestObject();
TestThis = new TestObject();
//Lets cast our object to a type of ‘TestInterface’
CastedTestInterface = TestThis.cast(‘TestInterface’);
Don’t take my word for it, here is a FULL WORKING EXAMPLE!
Now if you inspect CastedTestInterface you may notice that it is just an instance of the interface you created! Keep in mind though that it also holds a closure to the object it was casted from.
Lets try executing a method:
CastedTestInterface.testMethod(1,2,3);
Whoa! it worked!
Lets try executing another method specified by the interface:
CastedTestInterface.testMethod2(1,2,3);
Opps, we get an error because this method does not exist! We better fix that, humm but this means that the interface is working!
Long story short, if a method you call from a cast is missing from the object; you will be alerted when you call the method, an error will be thrown, and you scolded for doing such a horrible thing! To be honest, this method is 1/2 of an abstracted class and 1/2 of an interface, an {interstraction}?!?!
This is because most interfaces specify that they can not have implementation details in them, but ours do. On the other hand this is not a abstracted class! Because abstracted classes are extensions of the original, when clearly this type of OOP not an extension, but a subset. Hope you enjoyed the tutorial on how to create interfaces
.
