
Last night we were talking about frameworks, MachII, Gateways, and MVC. I
brought up another one of my back burner projects (yes, it is a large
burner) and a few people seemed kind of interested. It is a framework for
CFCs that was inspired by Objetive-C. Now, if you don’t know anything
about objective-c or small talk don’t sweat it - read on it’s pretty easy.
What makes Objective-C cooler than Java, in my opinion, is that
Objective-C does runtime binding and Java does compile time binding. It
sounds all fancy but what it boils down to in Java you will get compile
errors if a method on an object doesn’t exist. If you did
java.util.Map.hiThereBucky(); the class just wouldn’t compile. (You can
do a bit of runtime stuff with reflection, but that is a gnarly can of
worms that doesn’t work well)
Now at first compile time binding sounds good and proper. But if you
take a step back and pretend that objects in your projects are like
objects in reality, then it doesn’t quite hold up. I could do
GirlFriend.talkTo("word") and Wall.talkTo("word") in reality, but the
hierarchy for that in a classical Java or Coldfusion application would
be crazy.



What objective-C does (and Objective-CFML tries to do) is work in a
message based fashion. So the above example would be more like Rob
->talksTo -> GirlFriend and Rob -> talksTo -> Wall. The receiving object
decides if it can handle the message or not. If it can it does it’s
thing, and if not it sends a message back saying "I don’t understand".
So the GirlFriend object would respond (I hope :-o) and the Wall object
would ignore (or just send back) the message. The only way this is
possible is if all objects can accept any message.
The messages are Coldfusion structs which are easily converted into Java
maps. They are also good to pass in web services, or stick on message
queues (CFMX 7 supports this I hear). Inter server object invoking could
be improved if there was, say, and object loader on each side.
Using objects in this way also opens the door a bit wider for
dynamically loaded objects and object versioning. Try removing a method
from one of your CFCs right now and see what happens - in this model the
object with the removed method would just pass a message back to the
calling object saying "I don’t understand".

Ok, so that’s it in a nut shell.
Here is the code if you’d like to play with it or improve upon it. I wrote
it on BlueDragon6.1 and I haven’t run it on CFMX yet though I think it
should be fine. Here is a quick run down on how it’s used for those who
are curious but don’t want to download and play with it.
There are 3 objects right now CObject, CMarshaler, and
Nil. CObject is the base object and all objects must extend
this object. CObject is what does all the message translation and
a lot of the work for you. CMarshaler is meant to take an object
and serialize it to XML so you can take an object and dump it to disk
then recreate it in the state it was in before you saved it. Nil
is just a null object.
Any public or remote methods you add to your CFC will become handled
messages. if you add a single cfargument of type struct, the full
message will be passed into your method so you can get arguments out if
you require them. If the receiver of the message doesn’t know what to do
with the message it will call the method invalidMessage on the
object that sent the message so if you want to override the default
behavior for unknown messages override the method invalidMessage
in your CFC.
Here is an example usage (this assume thing1 and thing2 extend CObject)
//the object doing the sending
sender = createObject("component","thing1");
//an object to send to
receiver = createObject("component","thing2");
//create a message from the sender object
msg = sender.createMessage();
//*required* the method to invoke on the object
msg.method = "howDoYouDo";
//you can add whatever else you want to the message as the message
//will get passed in full into the object::method
msg.params = structNew();
msg.params.my_name = "fred";
writeOutput(receiver.handleMessage(msg));
Lot of talk for a little bit of code I know. Let me know what you think
or if this is totally stupid
By the way I was going to name this YAFF - Yet Another Framework - and
you get to add in the other F :) And most of the images you see here
were taken or tweeked from an
Apple MVC Paradigm article.