Computer Science Canada Rockin' the Object-Oriented Casbah? |
Author: | wtd [ Sun Jun 11, 2006 11:40 am ] |
Post subject: | Rockin' the Object-Oriented Casbah? |
So, you've learned about object-oriented programming. You "get" it. You're pounding out classes like they're going out of style. But are you as much of a guru as you think? Do you understand the following?
If this doesn't sound familiar, then ask questions. |
Author: | Clayton [ Sun Jun 11, 2006 1:56 pm ] |
Post subject: | |
what is multiple dispatch and prototype-based objects? |
Author: | Cervantes [ Sun Jun 11, 2006 3:05 pm ] |
Post subject: | |
I'm all for getting a lecture in any language or languages you want on any or all of these topics. |
Author: | wtd [ Sun Jun 11, 2006 3:06 pm ] | ||||
Post subject: | |||||
To demonstrate multiple dispatch, let me show you an example. First I'll write it in Java, and then in Common Lisp. Java has to simulate multiple dispatch with the Visitor Pattern. The Common Lisp Object System innately works based on multiple dispatch.
And in CLOS:
|
Author: | wtd [ Sun Jun 11, 2006 3:37 pm ] | ||||
Post subject: | |||||
A slightly more complex example:
|
Author: | wtd [ Mon Jun 12, 2006 3:42 pm ] | ||||||||
Post subject: | |||||||||
Now, as for Prototype based programming... Let's start with a simple class and object in Python for the heck of it.
Pretty simple, right? In Python, object is the root of the inheritance hierarchy, so Ninja inherits from it. Ninja is now a subclass of object and inherits from it all of its functionality. Then we instantiate Ninja to get a new Ninja object. Let's create a subclass of Ninja.
This also pretty simple. We've inherited from Ninja, so we call Ninja's init function on self to set up the strength, and then also add the "has_sword" variable. So we have two fundamental things going on: we can subclass another class, and we can instantiate a class to create a new object. What if we could take classes out of the picture and unify this into a single operation? With prototype-based languages like Io, we can. Io has Object, which is the basic object. To create anything more specialized we simply clone Object and add things to it.
Now here Object is considered a prototype of Ninja. If I send a message to Ninja that it does not understand, it will check to see if its prototypes do understand it. This permits "inheritance" in Io. So, we've seen "subclassing" in Io, but what about creating a Ninja object? Well, Ninja is already an object, so...
|
Author: | rizzix [ Mon Jun 12, 2006 8:15 pm ] |
Post subject: | |
Ah you should talk about prototype based mulitple dispatch. I don't know any prototype based language (not good enough anyway), but I believe it would be pretty neat. |